Quick experiment with quickjs.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3423 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2019-10-06 22:19:08 +00:00
parent c7b13dd1ae
commit d6018736d5
62 changed files with 91209 additions and 0 deletions

39
src/quickjstest.c Normal file
View File

@ -0,0 +1,39 @@
#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;
}