|
| 1 | +#include <Python.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include "libqjs.h" |
| 4 | + |
| 5 | + |
| 6 | +static PyObject* qjs_init(PyObject* self, PyObject *args) { |
| 7 | + |
| 8 | + return Py_BuildValue("b", 0); |
| 9 | +} |
| 10 | + |
| 11 | +static PyObject* qjs_runcode(PyObject* self, PyObject *args) { |
| 12 | + JSRuntime *rt; |
| 13 | + JSContext *ctx; |
| 14 | + const char *s; |
| 15 | + char *code; |
| 16 | + JSValue val, global_obj; |
| 17 | + int ret, tag, code_len, has_module=0, eval_flags; |
| 18 | + |
| 19 | + if (!PyArg_ParseTuple(args, "s", &code)) |
| 20 | + return NULL; |
| 21 | + code_len = strlen(code); |
| 22 | + rt = JS_NewRuntime(); |
| 23 | + ctx = JS_NewContext(rt); |
| 24 | + |
| 25 | + /* loader for ES6 modules */ |
| 26 | + JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL); |
| 27 | + |
| 28 | + js_std_add_helpers(ctx, 0, NULL); // int argc, char **argv |
| 29 | + |
| 30 | + /* system modules */ |
| 31 | + js_init_module_std(ctx, "std"); |
| 32 | + js_init_module_os(ctx, "os"); |
| 33 | + |
| 34 | + has_module = JS_DetectModule((const char *)code, code_len); |
| 35 | + |
| 36 | + if (has_module) |
| 37 | + eval_flags = JS_EVAL_TYPE_MODULE; |
| 38 | + else |
| 39 | + eval_flags = JS_EVAL_TYPE_GLOBAL; |
| 40 | + |
| 41 | + if ((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_MODULE) { |
| 42 | + /* for the modules, we compile then run to be able to set |
| 43 | + import.meta */ |
| 44 | + val = JS_Eval(ctx, code, code_len, "<input>", |
| 45 | + eval_flags | JS_EVAL_FLAG_COMPILE_ONLY); |
| 46 | + |
| 47 | + if (JS_IsException(val)) { |
| 48 | + |
| 49 | + js_std_dump_error(ctx); |
| 50 | + return NULL; |
| 51 | + } |
| 52 | + |
| 53 | + js_module_set_import_meta(ctx, val, 1, 1); |
| 54 | + val = JS_EvalFunction(ctx, val); |
| 55 | + // 如果包含模块,返回值要写入全局变量 |
| 56 | + global_obj = JS_GetGlobalObject(ctx); |
| 57 | + val = JS_GetPropertyStr(ctx, global_obj, "_r"); |
| 58 | + JS_FreeValue(ctx, global_obj); |
| 59 | + } else { // 不包含模块,直接运行 |
| 60 | + val = JS_Eval(ctx, code, code_len, "<input>", eval_flags); |
| 61 | + } |
| 62 | + // // buf, buf_len, filename, eval_flags |
| 63 | + // val = JS_Eval(ctx, code, strlen(code), "<input>", -1); |
| 64 | + |
| 65 | + if (JS_IsException(val)) { |
| 66 | + js_std_dump_error(ctx); |
| 67 | + return NULL; |
| 68 | + } |
| 69 | + |
| 70 | + tag = JS_VALUE_GET_TAG(val); |
| 71 | + |
| 72 | + s = JS_ToCString(ctx, val); |
| 73 | + |
| 74 | + JS_FreeValue(ctx, val); |
| 75 | + // printf("return val[%d]: %s \n", tag, s); |
| 76 | + |
| 77 | + js_std_free_handlers(rt); |
| 78 | + JS_FreeContext(ctx); |
| 79 | + JS_FreeRuntime(rt); |
| 80 | + |
| 81 | + return Py_BuildValue("s", s); |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +static PyMethodDef qjs_funcs[] = { |
| 86 | + {"init", (PyCFunction)qjs_init, METH_VARARGS, "helloworld_docs"}, |
| 87 | + {"eval_js", (PyCFunction)qjs_runcode, METH_VARARGS, "helloworld_docs"}, |
| 88 | + {NULL, NULL, 0, NULL} |
| 89 | +}; |
| 90 | + |
| 91 | +static PyModuleDef qjs_module = { |
| 92 | + PyModuleDef_HEAD_INIT, |
| 93 | + "qjs", /* name of module */ |
| 94 | + NULL, /* module documentation, may be NULL */ |
| 95 | + -1, /* size of per-interpreter state of the module, |
| 96 | + or -1 if the module keeps state in global variables. */ |
| 97 | + qjs_funcs |
| 98 | +}; |
| 99 | + |
| 100 | + |
| 101 | +PyMODINIT_FUNC PyInit_qjs() { |
| 102 | + return PyModule_Create(&qjs_module); |
| 103 | +} |
0 commit comments