fprintf(f, "\n");
}
+typedef enum {
+ CNAME_TYPE_SCRIPT,
+ CNAME_TYPE_MODULE,
+ CNAME_TYPE_JSON_MODULE,
+} CNameTypeEnum;
+
static void output_object_code(JSContext *ctx,
FILE *fo, JSValueConst obj, const char *c_name,
- BOOL load_only)
+ CNameTypeEnum c_name_type)
{
uint8_t *out_buf;
size_t out_buf_len;
int flags;
- flags = JS_WRITE_OBJ_BYTECODE;
+
+ if (c_name_type == CNAME_TYPE_JSON_MODULE)
+ flags = 0;
+ else
+ flags = JS_WRITE_OBJ_BYTECODE;
if (byte_swap)
flags |= JS_WRITE_OBJ_BSWAP;
out_buf = JS_WriteObject(ctx, &out_buf_len, obj, flags);
exit(1);
}
- namelist_add(&cname_list, c_name, NULL, load_only);
+ namelist_add(&cname_list, c_name, NULL, c_name_type);
fprintf(fo, "const uint32_t %s_size = %u;\n\n",
c_name, (unsigned int)out_buf_len);
}
JSModuleDef *jsc_module_loader(JSContext *ctx,
- const char *module_name, void *opaque)
+ const char *module_name, void *opaque,
+ JSValueConst attributes)
{
JSModuleDef *m;
namelist_entry_t *e;
} else {
size_t buf_len;
uint8_t *buf;
- JSValue func_val;
char cname[1024];
-
+ int res;
+
buf = js_load_file(ctx, &buf_len, module_name);
if (!buf) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s'",
return NULL;
}
- /* compile the module */
- func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
- JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
- js_free(ctx, buf);
- if (JS_IsException(func_val))
- return NULL;
- get_c_name(cname, sizeof(cname), module_name);
- if (namelist_find(&cname_list, cname)) {
- find_unique_cname(cname, sizeof(cname));
- }
- output_object_code(ctx, outfile, func_val, cname, TRUE);
+ res = js_module_test_json(ctx, attributes);
+ if (has_suffix(module_name, ".json") || res > 0) {
+ /* compile as JSON or JSON5 depending on "type" */
+ JSValue val;
+ int flags;
+
+ if (res == 2)
+ flags = JS_PARSE_JSON_EXT;
+ else
+ flags = 0;
+ val = JS_ParseJSON2(ctx, (char *)buf, buf_len, module_name, flags);
+ js_free(ctx, buf);
+ if (JS_IsException(val))
+ return NULL;
+ /* create a dummy module */
+ m = JS_NewCModule(ctx, module_name, js_module_dummy_init);
+ if (!m) {
+ JS_FreeValue(ctx, val);
+ return NULL;
+ }
+
+ get_c_name(cname, sizeof(cname), module_name);
+ if (namelist_find(&cname_list, cname)) {
+ find_unique_cname(cname, sizeof(cname));
+ }
+
+ /* output the module name */
+ fprintf(outfile, "static const uint8_t %s_module_name[] = {\n",
+ cname);
+ dump_hex(outfile, (const uint8_t *)module_name, strlen(module_name) + 1);
+ fprintf(outfile, "};\n\n");
- /* the module is already referenced, so we must free it */
- m = JS_VALUE_GET_PTR(func_val);
- JS_FreeValue(ctx, func_val);
+ output_object_code(ctx, outfile, val, cname, CNAME_TYPE_JSON_MODULE);
+ JS_FreeValue(ctx, val);
+ } else {
+ JSValue func_val;
+
+ /* compile the module */
+ func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
+ JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
+ js_free(ctx, buf);
+ if (JS_IsException(func_val))
+ return NULL;
+ get_c_name(cname, sizeof(cname), module_name);
+ if (namelist_find(&cname_list, cname)) {
+ find_unique_cname(cname, sizeof(cname));
+ }
+ output_object_code(ctx, outfile, func_val, cname, CNAME_TYPE_MODULE);
+
+ /* the module is already referenced, so we must free it */
+ m = JS_VALUE_GET_PTR(func_val);
+ JS_FreeValue(ctx, func_val);
+ }
}
return m;
}
} else {
get_c_name(c_name, sizeof(c_name), filename);
}
- output_object_code(ctx, fo, obj, c_name, FALSE);
+ output_object_code(ctx, fo, obj, c_name, CNAME_TYPE_SCRIPT);
JS_FreeValue(ctx, obj);
}
JS_SetStripInfo(rt, strip_flags);
/* loader for ES6 modules */
- JS_SetModuleLoaderFunc(rt, NULL, jsc_module_loader, NULL);
+ JS_SetModuleLoaderFunc2(rt, NULL, jsc_module_loader, NULL, NULL);
fprintf(fo, "/* File generated automatically by the QuickJS compiler. */\n"
"\n"
}
for(i = 0; i < dynamic_module_list.count; i++) {
- if (!jsc_module_loader(ctx, dynamic_module_list.array[i].name, NULL)) {
+ if (!jsc_module_loader(ctx, dynamic_module_list.array[i].name, NULL, JS_UNDEFINED)) {
fprintf(stderr, "Could not load dynamic module '%s'\n",
dynamic_module_list.array[i].name);
exit(1);
}
for(i = 0; i < cname_list.count; i++) {
namelist_entry_t *e = &cname_list.array[i];
- if (e->flags) {
+ if (e->flags == CNAME_TYPE_MODULE) {
fprintf(fo, " js_std_eval_binary(ctx, %s, %s_size, 1);\n",
e->name, e->name);
+ } else if (e->flags == CNAME_TYPE_JSON_MODULE) {
+ fprintf(fo, " js_std_eval_binary_json_module(ctx, %s, %s_size, (const char *)%s_module_name);\n",
+ e->name, e->name, e->name);
}
}
fprintf(fo,
for(i = 0; i < cname_list.count; i++) {
namelist_entry_t *e = &cname_list.array[i];
- if (!e->flags) {
+ if (e->flags == CNAME_TYPE_SCRIPT) {
fprintf(fo, " js_std_eval_binary(ctx, %s, %s_size, 0);\n",
e->name, e->name);
}
return 0;
}
+static JSModuleDef *create_json_module(JSContext *ctx, const char *module_name, JSValue val)
+{
+ JSModuleDef *m;
+ m = JS_NewCModule(ctx, module_name, json_module_init);
+ if (!m) {
+ JS_FreeValue(ctx, val);
+ return NULL;
+ }
+ /* only export the "default" symbol which will contain the JSON object */
+ JS_AddModuleExport(ctx, m, "default");
+ JS_SetModulePrivateValue(ctx, m, val);
+ return m;
+}
+
/* in order to conform with the specification, only the keys should be
tested and not the associated values. */
int js_module_check_attributes(JSContext *ctx, void *opaque,
return ret;
}
-/* return TRUE if the attributes indicate a JSON module */
-JS_BOOL js_module_test_json(JSContext *ctx, JSValueConst attributes)
+/* return > 0 if the attributes indicate a JSON module */
+int js_module_test_json(JSContext *ctx, JSValueConst attributes)
{
JSValue str;
const char *cstr;
if (!cstr)
return FALSE;
/* XXX: raise an error if unknown type ? */
- res = (len == 4 && !memcmp(cstr, "json", len));
+ if (len == 4 && !memcmp(cstr, "json", len)) {
+ res = 1;
+ } else if (len == 5 && !memcmp(cstr, "json5", len)) {
+ res = 2;
+ } else {
+ res = 0;
+ }
JS_FreeCString(ctx, cstr);
return res;
}
JSValueConst attributes)
{
JSModuleDef *m;
-
+ int res;
+
if (has_suffix(module_name, ".so")) {
m = js_module_loader_so(ctx, module_name);
} else {
module_name);
return NULL;
}
-
- if (has_suffix(module_name, ".json") ||
- js_module_test_json(ctx, attributes)) {
- /* compile as JSON */
+ res = js_module_test_json(ctx, attributes);
+ if (has_suffix(module_name, ".json") || res > 0) {
+ /* compile as JSON or JSON5 depending on "type" */
JSValue val;
- val = JS_ParseJSON(ctx, (char *)buf, buf_len, module_name);
+ int flags;
+ if (res == 2)
+ flags = JS_PARSE_JSON_EXT;
+ else
+ flags = 0;
+ val = JS_ParseJSON2(ctx, (char *)buf, buf_len, module_name, flags);
js_free(ctx, buf);
if (JS_IsException(val))
return NULL;
- m = JS_NewCModule(ctx, module_name, json_module_init);
- if (!m) {
- JS_FreeValue(ctx, val);
+ m = create_json_module(ctx, module_name, val);
+ if (!m)
return NULL;
- }
- /* only export the "default" symbol which will contain the JSON object */
- JS_AddModuleExport(ctx, m, "default");
- JS_SetModulePrivateValue(ctx, m, val);
} else {
JSValue func_val;
/* compile the module */
JS_FreeValue(ctx, val);
}
}
+
+void js_std_eval_binary_json_module(JSContext *ctx,
+ const uint8_t *buf, size_t buf_len,
+ const char *module_name)
+{
+ JSValue obj;
+ JSModuleDef *m;
+
+ obj = JS_ReadObject(ctx, buf, buf_len, 0);
+ if (JS_IsException(obj))
+ goto exception;
+ m = create_json_module(ctx, module_name, obj);
+ if (!m) {
+ exception:
+ js_std_dump_error(ctx);
+ exit(1);
+ }
+}
+