1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[smjs] Compilation fixes.

This commit is contained in:
Witold Filipczyk 2021-09-05 17:50:41 +02:00
parent 3026d8f25e
commit f692389917
5 changed files with 60 additions and 76 deletions

View File

@ -22,6 +22,8 @@
#include "util/file.h"
#include "util/string.h"
#include <js/CompilationAndEvaluation.h>
#include <js/SourceText.h>
#define SMJS_HOOKS_FILENAME "hooks.js"
@ -192,18 +194,27 @@ smjs_do_file(char *path)
opts.setNoScriptRval(true);
JS::RootedValue rval(smjs_ctx);
JS_BeginRequest(smjs_ctx);
JSCompartment *prev = JS_EnterCompartment(smjs_ctx, smjs_elinks_object);
JS::Realm *prev = JS::EnterRealm(smjs_ctx, smjs_elinks_object);
if (!add_file_to_string(&script, path)
|| false == JS::Evaluate(smjs_ctx, opts,
script.source, script.length, &rval)) {
if (add_file_to_string(&script, path)) {
JS::SourceText<mozilla::Utf8Unit> srcBuf;
if (!srcBuf.init(smjs_ctx, script.source, script.length, JS::SourceOwnership::Borrowed)) {
alert_smjs_error("error loading script file");
ret = 0;
} else {
if (!JS::Evaluate(smjs_ctx, opts,
srcBuf, &rval)) {
alert_smjs_error("error loading script file");
ret = 0;
}
}
} else {
alert_smjs_error("error loading script file");
ret = 0;
}
JS_LeaveCompartment(smjs_ctx, prev);
JS_EndRequest(smjs_ctx);
JS::LeaveRealm(smjs_ctx, prev);
done_string(&script);
return ret;
@ -252,7 +263,8 @@ init_smjs(struct module *module)
return;
}
JS::SetWarningReporter(smjs_ctx, error_reporter);
/// TODO
/// JS::SetWarningReporter(smjs_ctx, error_reporter);
smjs_init_global_object();
@ -299,65 +311,15 @@ JSString *
utf8_to_jsstring(JSContext *ctx, const char *str, int length)
{
size_t in_bytes;
const char *in_end;
size_t utf16_alloc;
char16_t *utf16;
size_t utf16_used;
JSString *jsstr;
if (length == -1)
in_bytes = strlen(str);
else
in_bytes = length;
/* Each byte of input can become at most one UTF-16 unit.
* Check whether the multiplication could overflow. */
assert(!needs_utf16_surrogates(UCS_REPLACEMENT_CHARACTER));
if (in_bytes > ((size_t) -1) / sizeof(char16_t)) {
#ifdef HAVE_JS_REPORTALLOCATIONOVERFLOW
JS_ReportAllocationOverflow(ctx);
#else
JS_ReportOutOfMemory(ctx);
#endif
return NULL;
}
utf16_alloc = in_bytes;
/* Use malloc because SpiderMonkey will handle the memory after
* this routine finishes. */
utf16 = malloc(utf16_alloc * sizeof(char16_t));
if (utf16 == NULL) {
JS_ReportOutOfMemory(ctx);
return NULL;
}
JS::ConstUTF8CharsZ utf8chars(str, in_bytes);
in_end = str + in_bytes;
utf16_used = 0;
for (;;) {
unicode_val_T unicode;
unicode = utf8_to_unicode((char **) &str, in_end);
if (unicode == UCS_NO_CHAR)
break;
if (needs_utf16_surrogates(unicode)) {
assert(utf16_alloc - utf16_used >= 2);
if_assert_failed { free(utf16); return NULL; }
utf16[utf16_used++] = get_utf16_high_surrogate(unicode);
utf16[utf16_used++] = get_utf16_low_surrogate(unicode);
} else {
assert(utf16_alloc - utf16_used >= 1);
if_assert_failed { free(utf16); return NULL; }
utf16[utf16_used++] = unicode;
}
}
jsstr = JS_NewUCString(ctx, utf16, utf16_used);
/* Do not free if JS_NewUCString was successful because it takes over
* handling of the memory. */
if (jsstr == NULL) free(utf16);
return jsstr;
return JS_NewStringCopyUTF8Z(ctx, utf8chars);
}
/** Convert a char16_t array to UTF-8 and append it to struct string.

View File

@ -30,8 +30,7 @@ static JSObject *
smjs_get_global_object(void)
{
assert(smjs_ctx);
JSAutoRequest ar(smjs_ctx);
JS::CompartmentOptions opts;
JS::RealmOptions opts;
// if (!JS::InitSelfHostedCode(smjs_ctx)) {
// return NULL;
@ -41,9 +40,9 @@ smjs_get_global_object(void)
if (!jsobj) return NULL;
new JSAutoCompartment(smjs_ctx, jsobj);
new JSAutoRealm(smjs_ctx, jsobj);
JS_InitStandardClasses(smjs_ctx, jsobj);
JS::InitRealmStandardClasses(smjs_ctx);
return jsobj;
}

View File

@ -33,8 +33,7 @@ script_hook_url(va_list ap, void *data)
if (*url == NULL) return EVENT_HOOK_STATUS_NEXT;
JS_BeginRequest(smjs_ctx);
JSCompartment *prev = JS_EnterCompartment(smjs_ctx, smjs_elinks_object);
JS::Realm *prev = JS::EnterRealm(smjs_ctx, smjs_elinks_object);
smjs_ses = ses;
args[2].setString(JS_NewStringCopyZ(smjs_ctx, *url));
@ -51,8 +50,7 @@ script_hook_url(va_list ap, void *data)
}
smjs_ses = NULL;
JS_LeaveCompartment(smjs_ctx, prev);
JS_EndRequest(smjs_ctx);
JS::LeaveRealm(smjs_ctx, prev);
return ret;
}
@ -67,8 +65,7 @@ script_hook_pre_format_html(va_list ap, void *data)
JS::Value args[4];
JS::RootedValue r_rval(smjs_ctx);
JS_BeginRequest(smjs_ctx);
JSCompartment *prev = JS_EnterCompartment(smjs_ctx, smjs_elinks_object);
JS::Realm *prev = JS::EnterRealm(smjs_ctx, smjs_elinks_object);
evhook_use_params(ses && cached);
@ -99,8 +96,7 @@ script_hook_pre_format_html(va_list ap, void *data)
end:
JS_LeaveCompartment(smjs_ctx, prev);
JS_EndRequest(smjs_ctx);
JS::LeaveRealm(smjs_ctx, prev);
smjs_ses = NULL;
return ret;
}

View File

@ -162,7 +162,7 @@ keymap_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS:
struct string event_name = NULL_STRING;
JSObject *jsobj = &hvp.toObject();
if (false == JS_ObjectIsFunction(ctx, jsobj))
if (false == JS_ObjectIsFunction(jsobj))
return false;
if (!init_string(&event_name)) return false;

View File

@ -44,9 +44,36 @@ enum terminal_prop {
TERMINAL_TAB,
};
static bool
terminal_get_property_tab(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct terminal *term;
term = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &terminal_class, NULL);
if (!term) return false; /* already detached */
JSObject *obj = smjs_get_session_array_object(term);
if (obj) {
args.rval().setObject(*obj);
} else {
args.rval().setUndefined();
}
return true;
}
static const JSPropertySpec terminal_props[] = {
{ "tab", TERMINAL_TAB, JSPROP_ENUMERATE | JSPROP_READONLY },
{ NULL }
JS_PSG("tab", terminal_get_property_tab, JSPROP_ENUMERATE),
JS_PS_END
};
/* @terminal_class.getProperty */
@ -210,7 +237,7 @@ static const JSClassOps terminal_array_ops = {
nullptr, // call
nullptr, // hasInstance
nullptr, // construct
nullptr // trace JS_GlobalObjectTraceHook
JS_GlobalObjectTraceHook
};
static const JSClass terminal_array_class = {