Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 51 additions & 42 deletions qjs/lib/eval.c
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
#include <string.h>
#include "../quickjs/quickjs.h"
#include <stdbool.h>
#include <string.h>

char *emscripten_run_script_string(const char *script);

static JSValue js_host(JSContext *ctx, JSValueConst jsThis, int argc, JSValueConst *argv)
{
const char *str = JS_ToCString(ctx, argv[0]);
const char *res = emscripten_run_script_string(str);
JS_FreeCString(ctx, str);
return JS_NewString(ctx,res);
}
static JSRuntime *global_runtime = NULL;
static JSContext *global_ctx = NULL;
static bool context_initialized = false;

static JSValue js_host(JSContext *ctx, JSValueConst jsThis, int argc,
JSValueConst *argv) {
const char *str = JS_ToCString(ctx, argv[0]);
const char *res = emscripten_run_script_string(str);
JS_FreeCString(ctx, str);
return JS_NewString(ctx, res);
}

void registerContext(JSContext *ctx){
void registerContext(JSContext *ctx) {
JSValue global = JS_GetGlobalObject(ctx);
JSValue console = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, global, "host", console);
JS_SetPropertyStr(ctx, console, "run", JS_NewCFunction(ctx, js_host, "run", 1));
JS_SetPropertyStr(ctx, console, "run",
JS_NewCFunction(ctx, js_host, "run", 1));
}

static void initialize_context() {
if (!global_runtime) {
global_runtime = JS_NewRuntime();
}
if (!global_ctx) {
global_ctx = JS_NewContext(global_runtime);
}
if (!context_initialized) {
registerContext(global_ctx);
context_initialized = 1;
}
}

const char *eval(uint8_t *bytes, int byteLength)
{
JSRuntime *runtime = JS_NewRuntime();
JSContext *ctx = JS_NewContext(runtime);
const char *eval(uint8_t *bytes, int byteLength) {
initialize_context();
JSContext *ctx = global_ctx;

registerContext(ctx);

JSValue object;
object = JS_ReadObject(ctx, bytes, byteLength, JS_READ_OBJ_BYTECODE);
JSValue object = JS_ReadObject(ctx, bytes, byteLength, JS_READ_OBJ_BYTECODE);

JSValue value = JS_EvalFunction(ctx, object);

if (JS_IsException(value))
{
if (JS_IsException(value)) {
JSValue realException = JS_GetException(ctx);
return JS_ToCString(ctx, realException);
}
Expand All @@ -41,46 +55,41 @@ const char *eval(uint8_t *bytes, int byteLength)
return JS_ToCString(ctx, value);
}

int bytecodelength(const char *str)
{
JSRuntime *runtime = JS_NewRuntime();
JSContext *ctx = JS_NewContext(runtime);
JSValue object = JS_Eval(ctx, str, strlen(str), "<evalScript>", JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAG_COMPILE_ONLY);
int bytecodelength(const char *str) {
initialize_context();
JSContext *ctx = global_ctx;
JSValue object = JS_Eval(ctx, str, strlen(str), "<evalScript>",
JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAG_COMPILE_ONLY);

registerContext(ctx);


if (JS_IsException(object))
{
if (JS_IsException(object)) {
JSValue realException = JS_GetException(ctx);
return 0;
}

size_t byteCodeLength;
uint8_t *bytes = JS_WriteObject(ctx, &byteCodeLength, object, JS_WRITE_OBJ_BYTECODE);
uint8_t *bytes =
JS_WriteObject(ctx, &byteCodeLength, object, JS_WRITE_OBJ_BYTECODE);

JS_FreeValue(ctx, object);
return byteCodeLength;
}

const char *bytecode(const char *str)
{
JSRuntime *runtime = JS_NewRuntime();
JSContext *ctx = JS_NewContext(runtime);
JSValue object = JS_Eval(ctx, str, strlen(str), "<evalScript>", JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAG_COMPILE_ONLY);

registerContext(ctx);
const char *bytecode(const char *str) {
initialize_context();
JSContext *ctx = global_ctx;
JSValue object = JS_Eval(ctx, str, strlen(str), "<evalScript>",
JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAG_COMPILE_ONLY);

if (JS_IsException(object))
{
if (JS_IsException(object)) {
JSValue realException = JS_GetException(ctx);
return JS_ToCString(ctx, realException);
}

size_t byteCodeLength;
uint8_t *bytes = JS_WriteObject(ctx, &byteCodeLength, object, JS_WRITE_OBJ_BYTECODE);
uint8_t *bytes =
JS_WriteObject(ctx, &byteCodeLength, object, JS_WRITE_OBJ_BYTECODE);

JS_FreeValue(ctx, object);

return bytes;
return (const char *)bytes;
}
2 changes: 1 addition & 1 deletion qjs/makefile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
emcc lib/eval.c ./quickjs/quickjs.c ./quickjs/cutils.c ./quickjs/libregexp.c ./quickjs/libbf.c ./quickjs/libunicode.c -o lib/eval.js -DCONFIG_VERSION="\"1.0.0\"" -s WASM=1 -s SINGLE_FILE -s MODULARIZE=1 -lm -Oz -s EXPORTED_FUNCTIONS='["_eval","_bytecode","_bytecodelength"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' --llvm-lto 3 -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s ENVIRONMENT='web'
emcc lib/eval.c ./quickjs/quickjs.c ./quickjs/cutils.c ./quickjs/libregexp.c ./quickjs/libbf.c ./quickjs/libunicode.c -o lib/eval.js -DCONFIG_VERSION="\"1.0.0\"" -s WASM=1 -s SINGLE_FILE -s MODULARIZE=1 -lm -Oz -s EXPORTED_FUNCTIONS='["_eval","_bytecode","_bytecodelength"]' -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap","HEAPU8"]' --llvm-lto 3 -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s ENVIRONMENT='web'