Skip to content

Commit

Permalink
Pass strings to C on the heap instead of the stack (bellard#32)
Browse files Browse the repository at this point in the history
* failing memory test

* pass strings on the heap
  • Loading branch information
justjake authored Sep 11, 2020
1 parent ff7cf8e commit e58ca7b
Show file tree
Hide file tree
Showing 9 changed files with 49,311 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ QUICKJS_DEFINES:=-D_GNU_SOURCE -DCONFIG_VERSION=\"$(QUICKJS_CONFIG_VERSION)\"
EMCC_EXPORTED_FUNCS+=-s EXPORTED_FUNCTIONS=$(shell cat $(BUILD_WRAPPER)/symbols.json)

CFLAGS_EMCC+=-s WASM=1
CFLAGS_EMCC+=-s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap", "addFunction", "removeFunction", "_malloc", "_free"]'
CFLAGS_EMCC+=-s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap", "addFunction", "removeFunction", "stringToUTF8", "lengthBytesUTF8", "_malloc", "_free"]'
CFLAGS_EMCC+=-s NODEJS_CATCH_EXIT=0
CFLAGS_EMCC+=-s MODULARIZE=1
CFLAGS_EMCC+=-s EXPORT_NAME=QuickJSRaw
Expand Down
15 changes: 13 additions & 2 deletions c/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
#define QTS_DUMP(value) ;
#endif

/**
* Signal to our FFI code generator that this string argument should be passed as a pointer
* allocated by the caller on the heap, not a JS string on the stack.
* https://github.com/emscripten-core/emscripten/issues/6860#issuecomment-405818401
*/
#define HeapChar const char

void qts_log(char* msg) {
fputs(PKG, stderr);
fputs(msg, stderr);
Expand Down Expand Up @@ -291,7 +298,7 @@ double QTS_GetFloat64(JSContext *ctx, JSValueConst *value) {
return result;
}

JSValue *QTS_NewString(JSContext *ctx, const char* string) {
JSValue *QTS_NewString(JSContext *ctx, HeapChar *string) {
return jsvalue_to_heap(JS_NewString(ctx, string));
}

Expand Down Expand Up @@ -434,7 +441,7 @@ char *QTS_Dump(JSContext *ctx, JSValueConst *obj) {
return QTS_GetString(ctx, obj);
}

JSValue *QTS_Eval(JSContext *ctx, const char* js_code) {
JSValue *QTS_Eval(JSContext *ctx, HeapChar *js_code) {
return jsvalue_to_heap(JS_Eval(ctx, js_code, strlen(js_code), "eval.js", JS_EVAL_TYPE_GLOBAL));
}

Expand Down Expand Up @@ -470,3 +477,7 @@ JSValue *QTS_NewPromiseCapability(JSContext *ctx, JSValue **resolve_funcs_out) {
resolve_funcs_out[1] = jsvalue_to_heap(resolve_funcs[1]);
return jsvalue_to_heap(promise);
}

void QTS_TestStringArg(const char *string) {
// pass
}
Loading

0 comments on commit e58ca7b

Please sign in to comment.