Skip to content

Commit

Permalink
reduce Windows warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosvm committed Jun 3, 2024
1 parent 5ed69e2 commit 653ee2c
Show file tree
Hide file tree
Showing 13 changed files with 2,993 additions and 90 deletions.
2,869 changes: 2,869 additions & 0 deletions 1qqq

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,12 @@ static inline void print_version(void)

static inline FILE *open_file(const char *filename, const char *mode)
{
FILE *stream = fopen(filename, mode);
FILE *stream = NULL;
#ifdef _WIN32
(void) fopen_s(&stream, filename, mode);
#else
stream = fopen(filename, mode);
#endif
if (!stream)
fatal_error("unable to open file `%s`", filename);
return stream;
Expand Down
27 changes: 18 additions & 9 deletions core/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#ifdef _WIN32
#include <windows.h>
#include <io.h>
#endif

#ifndef _WIN32
Expand Down Expand Up @@ -66,7 +67,12 @@ static void open_call(HkState *state, HkValue *args)
hk_return_if_not_ok(state);
HkString *filename = hk_as_string(args[1]);
HkString *mode = hk_as_string(args[2]);
FILE *stream = fopen(filename->chars, mode->chars);
FILE *stream = NULL;
#ifdef _WIN32
(void) fopen_s(&stream, filename->chars, mode->chars);
#else
stream = fopen(filename->chars, mode->chars);
#endif
if (!stream)
{
hk_state_push_nil(state);
Expand Down Expand Up @@ -133,11 +139,13 @@ static void sync_call(HkState *state, HkValue *args)
hk_state_check_argument_userdata(state, args, 1);
hk_return_if_not_ok(state);
FILE *stream = ((File *) hk_as_userdata(args[1]))->stream;
int fd = fileno(stream);
bool result;
#ifdef _WIN32
result = FlushFileBuffers(fd);
int fd = _fileno(stream);
HANDLE handle = (HANDLE) _get_osfhandle(fd);
result = FlushFileBuffers(handle);
#else
int fd = fileno(stream);
result = !fsync(fd);
#endif
hk_state_push_bool(state, result);
Expand Down Expand Up @@ -169,7 +177,7 @@ static void seek_call(HkState *state, HkValue *args)
hk_state_check_argument_int(state, args, 3);
hk_return_if_not_ok(state);
FILE *stream = ((File *) hk_as_userdata(args[1]))->stream;
int64_t offset = (int64_t) hk_as_number(args[2]);
long offset = (long) hk_as_number(args[2]);
int whence = (int) hk_as_number(args[3]);
hk_state_push_number(state, fseek(stream, offset, whence));
}
Expand All @@ -181,7 +189,7 @@ static void read_call(HkState *state, HkValue *args)
hk_state_check_argument_int(state, args, 2);
hk_return_if_not_ok(state);
FILE *stream = ((File *) hk_as_userdata(args[1]))->stream;
int64_t size = (int64_t) hk_as_number(args[2]);
int size = (int) hk_as_number(args[2]);
HkString *str = hk_string_new_with_capacity(size);
int length = (int) fread(str->chars, 1, size, stream);
if (length < size && !feof(stream))
Expand All @@ -205,8 +213,8 @@ static void write_call(HkState *state, HkValue *args)
hk_return_if_not_ok(state);
FILE *stream = ((File *) hk_as_userdata(args[1]))->stream;
HkString *str = hk_as_string(args[2]);
size_t size = str->length;
if (fwrite(str->chars, 1, size, stream) < size)
int size = str->length;
if ((int) fwrite(str->chars, 1, size, stream) < size)
{
hk_state_push_nil(state);
return;
Expand All @@ -230,8 +238,9 @@ static void writeln_call(HkState *state, HkValue *args)
hk_return_if_not_ok(state);
FILE *stream = ((File *) hk_as_userdata(args[1]))->stream;
HkString *str = hk_as_string(args[2]);
size_t size = str->length;
if (fwrite(str->chars, 1, size, stream) < size || fwrite("\n", 1, 1, stream) < 1)
int size = str->length;
if ((int) fwrite(str->chars, 1, size, stream) < size
|| fwrite("\n", 1, 1, stream) < 1)
{
hk_state_push_nil(state);
return;
Expand Down
6 changes: 3 additions & 3 deletions core/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ static inline cJSON *value_to_json(HkValue val)
for (int i = 0; i < ztruct->length; ++i)
{
HkField field = fields[i];
HkValue val = inst->values[i];
cJSON *json_val = value_to_json(val);
hk_assert(cJSON_AddItemToObject(json, field.name->chars, json_val), "Failed to add item to object.");
cJSON *json_val = value_to_json(inst->values[i]);
hk_assert(cJSON_AddItemToObject(json, field.name->chars, json_val),
"Failed to add item to object.");
}
}
break;
Expand Down
4 changes: 2 additions & 2 deletions core/selectors.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static inline bool poll_selector_register(PollSelector *selector,
if (selector->count == MAX_FDS) return false;
PollFd fd = {
.fd = (int) udata->sock,
.events = events,
.events = (short) events,
.revents = 0
};
int index = selector->count;
Expand Down Expand Up @@ -149,7 +149,7 @@ static inline bool poll_selector_modify(PollSelector *selector,
if (fd->fd == (int) udata->sock) break;
}
if (i == n) return false;
selector->fds[i].events = events;
selector->fds[i].events = (short) events;
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion core/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static void bind_call(HkState *state, HkValue *args)
}
struct sockaddr_in sock_addr;
memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = udata->domain;
sock_addr.sin_family = (unsigned short) udata->domain;
sock_addr.sin_port = htons((uint16_t) port);
if (inet_pton(AF_INET, address, &sock_addr.sin_addr) < 1)
{
Expand Down
21 changes: 13 additions & 8 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ static inline HkArray *split(HkString *str, HkString *sep)
// TODO: Do not use strtok_r and do not copy the string
HkString *_str = hk_string_copy(str);
char *cur = _str->chars;
char *tk;
while ((tk = strtok_r(cur, sep->chars, &cur)))
char *tk = strtok_r(cur, sep->chars, &cur);
while (tk)
{
HkValue elem = hk_string_value(hk_string_from_chars(-1, tk));
hk_array_inplace_add_element(arr, elem);
tk = strtok_r(cur, sep->chars, &cur);
}
hk_string_free(_str);
return arr;
Expand Down Expand Up @@ -267,13 +268,13 @@ static void to_int_call(HkState *state, HkValue *args)
HkValue val = args[1];
if (hk_is_number(val))
{
hk_state_push_number(state, (int64_t) hk_as_number(val));
hk_state_push_number(state, (double) ((int64_t) hk_as_number(val)));
return;
}
double result;
string_to_double(state, hk_as_string(val), &result);
hk_return_if_not_ok(state);
hk_state_push_number(state, (int64_t) result);
hk_state_push_number(state, (double) ((int64_t) result));
}

static void to_number_call(HkState *state, HkValue *args)
Expand Down Expand Up @@ -405,7 +406,11 @@ static void bin_call(HkState *state, HkValue *args)
char *chars = str->chars;
for (int i = 0; i < length; ++i)
{
#ifdef _WIN32
sscanf_s(chars, "%2hhx", (unsigned char *) &result->chars[i]);
#else
sscanf(chars, "%2hhx", (unsigned char *) &result->chars[i]);
#endif
chars += 2;
}
hk_state_push_string(state, result);
Expand Down Expand Up @@ -466,14 +471,14 @@ static void len_call(HkState *state, HkValue *args)
HkRange *range = hk_as_range(val);
if (range->start < range->end)
{
int result = (int) range->end - range->start + 1;
hk_state_push_number(state, result);
int64_t result = range->end - range->start + 1;
hk_state_push_number(state, (double) result);
return;
}
if (range->start > range->end)
{
int result = (int) range->start - range->end + 1;
hk_state_push_number(state, result);
int64_t result = range->start - range->end + 1;
hk_state_push_number(state, (double) result);
return;
}
hk_state_push_number(state, 1);
Expand Down
9 changes: 4 additions & 5 deletions src/callable.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,10 @@ HkFunction *hk_function_deserialize(FILE *stream)
HkFunction **functions = (HkFunction **) hk_allocate(sizeof(*functions) * fn->functionsCapacity);
for (int i = 0; i < fn->functionsLength; ++i)
{
HkFunction *fn = hk_function_deserialize(stream);
if (!fn)
return NULL;
hk_incr_ref(fn);
functions[i] = fn;
HkFunction *nestedFn = hk_function_deserialize(stream);
if (!nestedFn) return NULL;
hk_incr_ref(nestedFn);
functions[i] = nestedFn;
}
fn->functions = functions;
if (fread(&fn->numNonlocals, sizeof(fn->numNonlocals), 1, stream) != 1)
Expand Down
24 changes: 13 additions & 11 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ typedef enum

#define consume(c, k) do \
{ \
Scanner *scan = (c)->scan; \
if (!match(scan, k)) \
if (!match((c)->scan, k)) \
syntax_error_unexpected(c); \
scanner_next_token(scan); \
scanner_next_token((c)->scan); \
} while(0)

#define add_placeholder(c) do \
Expand Down Expand Up @@ -604,10 +603,10 @@ static void compile_import_statement(Compiler *comp)
scanner_next_token(scan);
if (!match(scan, TOKEN_KIND_NAME))
syntax_error_unexpected(comp);
Token tk = scan->token;
tk = scan->token;
scanner_next_token(scan);
define_local(comp, &tk, false);
uint8_t index = add_string_constant(comp, &tk);
index = add_string_constant(comp, &tk);
hk_chunk_emit_opcode(chunk, HK_OP_CONSTANT);
hk_chunk_emit_byte(chunk, index);
++n;
Expand Down Expand Up @@ -697,11 +696,11 @@ static void compile_constant_declaration(Compiler *comp)
scanner_next_token(scan);
if (!match(scan, TOKEN_KIND_NAME))
syntax_error_unexpected(comp);
Token tk = scan->token;
tk = scan->token;
scanner_next_token(scan);
// FIXME: This is a bug, we should not define the local here
define_local(comp, &tk, false);
uint8_t index = add_string_constant(comp, &tk);
index = add_string_constant(comp, &tk);
hk_chunk_emit_opcode(chunk, HK_OP_CONSTANT);
hk_chunk_emit_byte(chunk, index);
++n;
Expand Down Expand Up @@ -790,11 +789,11 @@ static void compile_variable_declaration(Compiler *comp)
scanner_next_token(scan);
if (!match(scan, TOKEN_KIND_NAME))
syntax_error_unexpected(comp);
Token tk = scan->token;
tk = scan->token;
scanner_next_token(scan);
// FIXME: This is a bug, we should not define the local here
define_local(comp, &tk, true);
uint8_t index = add_string_constant(comp, &tk);
index = add_string_constant(comp, &tk);
hk_chunk_emit_opcode(chunk, HK_OP_CONSTANT);
hk_chunk_emit_byte(chunk, index);
++n;
Expand Down Expand Up @@ -1478,7 +1477,7 @@ static void compile_for_statement(Compiler *comp)
}
uint16_t jump1 = (uint16_t) chunk->codeLength;
bool missing = match(scan, TOKEN_KIND_SEMICOLON);
int offset1;
int offset1 = 0;
if (missing)
scanner_next_token(scan);
else
Expand Down Expand Up @@ -1509,7 +1508,10 @@ static void compile_for_statement(Compiler *comp)
hk_chunk_emit_opcode(chunk, HK_OP_JUMP);
hk_chunk_emit_word(chunk, jump2);
if (!missing)
{
hk_assert(offset1, "offset1 is zero");
patch_jump(comp, offset1);
}
end_loop(comp);
pop_scope(comp);
}
Expand Down Expand Up @@ -2254,7 +2256,7 @@ static Variable compile_variable(Compiler *comp, Token *tk, bool emit)
return (Variable) {
.isLocal = false,
.depth = -1,
.index = index,
.index = (uint8_t) index,
.length = tk->length,
.start = tk->start,
.isMutable = false
Expand Down
4 changes: 2 additions & 2 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,6 @@ void hk_dump(HkFunction *fn, FILE *stream)
}
}
fprintf(stream, "; %d instruction(s)\n\n", n);
for (int i = 0; i < fn->functionsLength; ++i)
hk_dump(fn->functions[i], stream);
for (int j = 0; j < fn->functionsLength; ++j)
hk_dump(fn->functions[j], stream);
}
Loading

0 comments on commit 653ee2c

Please sign in to comment.