40 lines
899 B
C
40 lines
899 B
C
|
#include "quickjs.h"
|
||
|
#include "quickjs-libc.h"
|
||
|
|
||
|
#include <string.h>
|
||
|
#include <stdio.h>
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
JSRuntime* runtime = JS_NewRuntime();
|
||
|
JSContext* context = JS_NewContext(runtime);
|
||
|
js_init_module_std(context, "std");
|
||
|
const char* import = "import * as std from 'std';\nglobalThis.std = std;\n";
|
||
|
JS_Eval(context, import, strlen(import), "<input>", JS_EVAL_TYPE_MODULE);
|
||
|
|
||
|
js_std_add_helpers(context, 0, NULL);
|
||
|
const char* js = "std.out.puts(\"hello\"); 5+4";
|
||
|
JSValue result = JS_Eval(context, js, strlen(js), "test.js", 0);
|
||
|
if (JS_IsError(context, result))
|
||
|
{
|
||
|
printf("got an error\n");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
printf("not an error\n");
|
||
|
}
|
||
|
if (JS_IsException(result))
|
||
|
{
|
||
|
js_std_dump_error(context);
|
||
|
}
|
||
|
const char* c = JS_ToCString(context, JS_ToString(context, result));
|
||
|
printf("c = %p\n", c);
|
||
|
if (c)
|
||
|
{
|
||
|
printf("%s\n", c);
|
||
|
}
|
||
|
JS_FreeContext(context);
|
||
|
JS_FreeRuntime(runtime);
|
||
|
return 0;
|
||
|
}
|