added '#cstr' strings for easier c-strings
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 8 Dec 2021 18:46:12 +0000 (12:46 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 8 Dec 2021 18:46:12 +0000 (12:46 -0600)
build.sh
include/astnodes.h
include/onyx_library.h
modules/glfw3/module.onyx
src/builtins.c
src/parser.c
src/symres.c
src/wasm_emit.c

index e7a2bedb84d37f937856afc7879c8aaa810ed921..5defd7c16e1189a3e2621cdb6468f4306cb9cf27 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -44,7 +44,7 @@ else
 fi
 
 C_FILES="$C_FILES wasm_runtime"
-FLAGS="$FLAGS -DENABLE_RUN_WITH_WASMER -rdynamic"
+FLAGS="$FLAGS -DENABLE_RUN_WITH_WASMER"
 LIBS="-L$CORE_DIR/lib -lwasmer -Wl,-rpath=$CORE_DIR/lib:./ -lpthread -ldl"
 INCLUDES="-I$WASMER_INCLUDE_DIR"
 
index d31f57e8150401c2b0bbc022a859e720ffdb48ff..fdf729c89682f496546316682a68677de9d1547b 100644 (file)
@@ -583,7 +583,7 @@ struct AstTyped { AstTyped_base; };
 struct AstNamedValue    { AstTyped_base; AstTyped* value; };
 struct AstUnaryOp       { AstTyped_base; UnaryOp operation; AstTyped *expr; };
 struct AstNumLit        { AstTyped_base; union { i32 i; i64 l; f32 f; f64 d; } value; };
-struct AstStrLit        { AstTyped_base; u64 addr; u64 length; };
+struct AstStrLit        { AstTyped_base; u64 addr; u64 length; b32 is_cstr: 1; };
 struct AstLocal         { AstTyped_base; };
 struct AstDereference   { AstTyped_base; AstTyped *expr; };
 struct AstSizeOf        { AstTyped_base; AstType *so_ast_type; Type *so_type; u64 size; };
@@ -1537,6 +1537,7 @@ extern AstGlobal builtin_stack_top;
 extern AstGlobal builtin_tls_base;
 extern AstGlobal builtin_tls_size;
 extern AstType  *builtin_string_type;
+extern AstType  *builtin_cstring_type;
 extern AstType  *builtin_range_type;
 extern Type     *builtin_range_type_type;
 extern AstType  *builtin_vararg_type;
index 2c717573a41f67356bfcc12a885f15ce8131637d..c5bc9c22e6a556530adf94a739b767b1ab7d139c 100644 (file)
@@ -24,7 +24,7 @@ typedef struct OnyxRuntime {
     char* (*wasm_memory_data)(wasm_memory_t *wasm_memory);
     wasm_extern_t* (*wasm_extern_lookup_by_name)(wasm_module_t* module, wasm_instance_t* instance, const char* name);
     wasm_func_t* (*wasm_extern_as_func)(wasm_extern_t* ext);
-    wasm_trap_t* (*wasm_func_call)(wasm_func_t* wasm_func, wasm_val_vec_t* args, wasm_val_vec_t* results);
+    wasm_trap_t* (*wasm_func_call)(const wasm_func_t* wasm_func, const wasm_val_vec_t* args, wasm_val_vec_t* results);
 } OnyxRuntime;
 
 OnyxRuntime* runtime;
index f293f82c00b74608b0f486241945f24c272bc4ab..f8922c0c5cad38f7409449b528159dc737ef78bf 100644 (file)
@@ -57,12 +57,12 @@ GLFWgammaramp :: struct {
     glfwDestroyCursor :: (cursor: GLFWcursor_p) -> void ---
     glfwSetCursor :: (window: GLFWwindow_p, cursor: GLFWcursor_p) -> void ---
     glfwSetKeyCallback :: (window: GLFWwindow_p, export_name: str) -> void ---
-    // glfwSetCharCallback
-    // glfwSetCharModsCallback
-    // glfwSetMouseButtonCallback
-    // glfwSetCursorPosCallback
-    // glfwSetCursorEnterCallback
-    // glfwSetScrollCallback
+    glfwSetCharCallback :: (window: GLFWwindow_p, export_name: str) -> void ---
+    glfwSetCharModsCallback :: (window: GLFWwindow_p, export_name: str) -> void ---
+    glfwSetMouseButtonCallback :: (window: GLFWwindow_p, export_name: str) -> void ---
+    glfwSetCursorPosCallback :: (window: GLFWwindow_p, export_name: str) -> void ---
+    glfwSetCursorEnterCallback :: (window: GLFWwindow_p, export_name: str) -> void ---
+    glfwSetScrollCallback :: (window: GLFWwindow_p, export_name: str) -> void ---
     // glfwSetDropCallback
     glfwJoystickPresent :: (jid: i32) -> i32 ---
     // glfwGetJoystickAxes :: (jid: i32, count: ^i32) -> ^f32 ---
index 515c133fec37dcc0757d2866d1a64be5c538b62d..b1ded77d1d02d82af4728ef9ce9bb4cf52177405 100644 (file)
@@ -48,6 +48,7 @@ AstGlobal builtin_tls_base    = { Ast_Kind_Global, 0, &builtin_tls_base_token, N
 AstGlobal builtin_tls_size    = { Ast_Kind_Global, 0, &builtin_tls_size_token, NULL, NULL, (AstType *) &basic_type_u32, NULL };
 
 AstType  *builtin_string_type;
+AstType  *builtin_cstring_type;
 AstType  *builtin_range_type;
 Type     *builtin_range_type_type;
 AstType  *builtin_vararg_type;
@@ -386,6 +387,12 @@ void initialize_builtins(bh_allocator a) {
         return;
     }
 
+    builtin_cstring_type = (AstType *) symbol_raw_resolve(p->scope, "cstr");
+    if (builtin_cstring_type == NULL) {
+        onyx_report_error((OnyxFilePos) { 0 }, "'cstr' type not found in builtin package.");
+        return;
+    }
+
     builtin_range_type = (AstType *) symbol_raw_resolve(p->scope, "range");
     if (builtin_range_type == NULL) {
         onyx_report_error((OnyxFilePos) { 0 }, "'range' struct not found in builtin package.");
index c7d7a2e65893a3cda4c3a6f84393e66b4005ec7b..81cfee8fb1f5e712c6006020289db28c84af428d 100644 (file)
@@ -665,7 +665,7 @@ static AstTyped* parse_factor(OnyxParser* parser) {
 
                 assert(builtin_code_type != NULL);
                 code_block->type_node = builtin_code_type;
-                
+
                 code_block->code = (AstNode *) parse_expression(parser, 0);
 
                 expect_token(parser, ')');
@@ -681,6 +681,19 @@ static AstTyped* parse_factor(OnyxParser* parser) {
                 retval = (AstTyped *) insert;
                 break;
             }
+            else if (parse_possible_directive(parser, "cstr")) {
+                // Copy pasted from above.
+                AstStrLit* str_node = make_node(AstStrLit, Ast_Kind_StrLit);
+                str_node->token     = expect_token(parser, Token_Type_Literal_String);
+                str_node->addr      = 0;
+                str_node->flags    |= Ast_Flag_Comptime;
+                str_node->is_cstr   = 1;
+
+                ENTITY_SUBMIT(str_node);
+
+                retval = (AstTyped *) str_node;
+                break;
+            }
 
             onyx_report_error(parser->curr->pos, "Invalid directive in expression.");
             return NULL;
index d58e802ae2eafed1c775dae521b793cefe198314..9c99f22a36e82e6a300204987f6ee8c54912effc 100644 (file)
@@ -476,10 +476,18 @@ static SymresStatus symres_expression(AstTyped** expr) {
             SYMRES(type, &(*expr)->type_node);
             break;
 
-        case Ast_Kind_StrLit:
-            SYMRES(type, &builtin_string_type);
-            (*expr)->type_node = builtin_string_type;
+        case Ast_Kind_StrLit: {
+            AstStrLit* str = (AstStrLit *) *expr;
+            if (str->is_cstr) {
+                SYMRES(type, &builtin_cstring_type);
+                str->type_node = builtin_cstring_type;
+
+            } else {
+                SYMRES(type, &builtin_string_type);
+                str->type_node = builtin_string_type;
+            }
             break;
+        }
 
         case Ast_Kind_Slice:
         case Ast_Kind_Subscript:
index d8cd5c6813c62901a09897629fd4ed1e6325524d..7a4f2d11f4186c3b60eb927a1916f9d95e29e731 100644 (file)
@@ -2676,7 +2676,9 @@ EMIT_FUNC(expression, AstTyped* expr) {
 
         case Ast_Kind_StrLit: {
             WID(WI_PTR_CONST, ((AstStrLit *) expr)->addr);
-            WID(WI_I32_CONST, ((AstStrLit *) expr)->length);
+
+            if (((AstStrLit *) expr)->is_cstr == 0)
+                WID(WI_I32_CONST, ((AstStrLit *) expr)->length);
             break;
         }
 
@@ -3424,21 +3426,22 @@ static void emit_string_literal(OnyxWasmModule* mod, AstStrLit* strlit) {
     if (index != -1) {
         StrLitInfo sti = mod->string_literals[index].value;
         strlit->addr   = sti.addr;
-        strlit->length = sti.len;
+        strlit->length = sti.len + (strlit->is_cstr ? 1 : 0);
 
         bh_free(global_heap_allocator, strdata);
         return;
     }
 
+    u32 actual_length = length + (strlit->is_cstr ? 1 : 0);
     WasmDatum datum = {
         .offset = mod->next_datum_offset,
-        .length = length,
+        .length = actual_length,
         .data = strdata,
     };
 
     strlit->addr = (u32) mod->next_datum_offset,
     strlit->length = length;
-    mod->next_datum_offset += length;
+    mod->next_datum_offset += actual_length;
 
     shput(mod->string_literals, (char *) strdata, ((StrLitInfo) { strlit->addr, strlit->length }));