Skip to content

Commit

Permalink
quickjs_backend.cpp: Additional sanity checks
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Nov 16, 2023
1 parent 547690f commit 232a703
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/quickjs_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2664,9 +2664,15 @@ bool QuickJS_EnumerateObjectProperties(JSContext *ctx, JSValue obj, const std::f

const char *key = JS_AtomToCString(ctx, atom);

func(key, atom);

JS_FreeCString(ctx, key);
if (key)
{
func(key, atom);
JS_FreeCString(ctx, key);
}
else
{
debug(LOG_INFO, "JS_AtomToCString returned null?");
}
}
for (int i = 0; i < count; i++)
{
Expand Down Expand Up @@ -2725,6 +2731,7 @@ bool quickjs_scripting_instance::loadScript(const WzString& path, int player, in
// Remember internal, reserved names
std::unordered_set<std::string>& internalNamespaceRef = internalNamespace;
QuickJS_EnumerateObjectProperties(ctx, global_obj, [&internalNamespaceRef](const char *key, JSAtom &) {
if (!key) { return; }
internalNamespaceRef.insert(key);
}, false);

Expand Down Expand Up @@ -2756,11 +2763,18 @@ bool quickjs_scripting_instance::saveScriptGlobals(nlohmann::json &result)
QuickJS_EnumerateObjectProperties(ctx, global_obj, [this, &result](const char *key, JSAtom &atom) {
JSValue jsVal = JS_GetProperty(ctx, global_obj, atom);
std::string nameStr = key;
if (internalNamespace.count(nameStr) == 0 && !JS_IsFunction(ctx, jsVal)
&& !JS_IsConstructor(ctx, jsVal)
)//&& !it.value().equals(engine->globalObject()))
if (!JS_IsException(jsVal))
{
if (internalNamespace.count(nameStr) == 0 && !JS_IsFunction(ctx, jsVal)
&& !JS_IsConstructor(ctx, jsVal)
)//&& !it.value().equals(engine->globalObject()))
{
result[nameStr] = JSContextValue{ctx, jsVal, true};
}
}
else
{
result[nameStr] = JSContextValue{ctx, jsVal, true};
debug(LOG_INFO, "Got an exception trying to get the value of \"%s\"?", nameStr.c_str());
}
JS_FreeValue(ctx, jsVal);
});
Expand Down Expand Up @@ -3578,13 +3592,20 @@ void to_json(nlohmann::json& j, const JSContextValue& v) {
QuickJS_EnumerateObjectProperties(v.ctx, v.value, [v, &j](const char *key, JSAtom &atom) {
JSValue jsVal = JS_GetProperty(v.ctx, v.value, atom);
std::string nameStr = key;
if (!JS_IsConstructor(v.ctx, jsVal))
if (!JS_IsException(jsVal))
{
j[nameStr] = JSContextValue{v.ctx, jsVal, v.skip_constructors};
if (!JS_IsConstructor(v.ctx, jsVal))
{
j[nameStr] = JSContextValue{v.ctx, jsVal, v.skip_constructors};
}
else if (!v.skip_constructors)
{
j[nameStr] = "<constructor>";
}
}
else if (!v.skip_constructors)
else
{
j[nameStr] = "<constructor>";
debug(LOG_INFO, "Got an exception trying to get the value of \"%s\"?", nameStr.c_str());
}
JS_FreeValue(v.ctx, jsVal);
}, false);
Expand Down

0 comments on commit 232a703

Please sign in to comment.