Wrap JSAutoRequest and replace usage of JSContext* with the wrapper.

JSAutoRequest is required before calling into most JSAPI methods, for GC
reasons.
Calling it is required and fragile as one must not forget.
Further, SM52 and later make manipulating JSContext* dangerous as that
can cross Compartment(Realm in SM68) barriers (and ScriptInterface now
matches a Compartment).

The solution to both problems is to avoid using JSContext* in 0 A.D.
itself. To achieve this, a Request class is introduced, and must be used
to access a JSContext* from a scriptInterface. Further, Request is
passed to other ScriptInterface functions isntead of JSContext*, making
it obvious that the caller has already called it, reducing errors and
redundant JSAutoRequest calls.
Only JSNative functions now get a naked JSContext* without protection,
but the likelihood of forgetting a request is lower since many
ScriptInterface functions now expect it.

JSContext* is directly passed to JSAPI functions only.

Part of the SM52 migration, stage: SM45 compatible

Based on a patch by: Itms
Tested By: Freagarach
Refs #4893

Differential Revision: https://code.wildfiregames.com/D3088
This was SVN commit r24176.
This commit is contained in:
wraitii
2020-11-13 13:18:22 +00:00
parent 6a029d2a84
commit ee0d204bf6
74 changed files with 1591 additions and 1812 deletions
@@ -99,26 +99,25 @@ public:
TS_ASSERT(tempMan != NULL);
tempMan->DisableValidation();
JSContext* cx = man.GetScriptInterface().GetContext();
JSAutoRequest rq(cx);
ScriptInterface::Request rq(man.GetScriptInterface());
// This is testing some bugs in the template JS object caching
const CParamNode* inherit1 = tempMan->LoadTemplate(ent2, "inherit1");
JS::RootedValue val(cx);
ScriptInterface::ToJSVal(cx, &val, inherit1);
JS::RootedValue val(rq.cx);
ScriptInterface::ToJSVal(rq, &val, inherit1);
TS_ASSERT_STR_EQUALS(man.GetScriptInterface().ToString(&val), "({Test1A:{'@a':\"a1\", '@b':\"b1\", '@c':\"c1\", d:\"d1\", e:\"e1\", f:\"f1\"}})");
const CParamNode* inherit2 = tempMan->LoadTemplate(ent2, "inherit2");
ScriptInterface::ToJSVal(cx, &val, inherit2);
ScriptInterface::ToJSVal(rq, &val, inherit2);
TS_ASSERT_STR_EQUALS(man.GetScriptInterface().ToString(&val), "({'@parent':\"inherit1\", Test1A:{'@a':\"a2\", '@b':\"b1\", '@c':\"c1\", d:\"d2\", e:\"e1\", f:\"f1\", g:\"g2\"}})");
const CParamNode* actor = tempMan->LoadTemplate(ent2, "actor|example1");
ScriptInterface::ToJSVal(cx, &val, &actor->GetChild("VisualActor"));
ScriptInterface::ToJSVal(rq, &val, &actor->GetChild("VisualActor"));
TS_ASSERT_STR_EQUALS(man.GetScriptInterface().ToString(&val), "({Actor:\"example1\", ActorOnly:(void 0), SilhouetteDisplay:\"false\", SilhouetteOccluder:\"false\", VisibleInAtlasOnly:\"false\"})");
const CParamNode* foundation = tempMan->LoadTemplate(ent2, "foundation|actor|example1");
ScriptInterface::ToJSVal(cx, &val, &foundation->GetChild("VisualActor"));
ScriptInterface::ToJSVal(rq, &val, &foundation->GetChild("VisualActor"));
TS_ASSERT_STR_EQUALS(man.GetScriptInterface().ToString(&val), "({Actor:\"example1\", ActorOnly:(void 0), Foundation:(void 0), SilhouetteDisplay:\"false\", SilhouetteOccluder:\"false\", VisibleInAtlasOnly:\"false\"})");
#define GET_FIRST_ELEMENT(n, templateName) \
@@ -127,7 +126,7 @@ public:
{ \
if (it->first[0] == '@') \
continue; \
ScriptInterface::ToJSVal(cx, &val, it->second); \
ScriptInterface::ToJSVal(rq, &val, it->second); \
break; \
}
+9 -12
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games.
/* Copyright (C) 2020 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -292,10 +292,9 @@ public:
void helper_script_roundtrip(const char* msg, const char* input, const char* expected, size_t expstreamlen = 0, const char* expstream = NULL, const char* debug = NULL)
{
ScriptInterface script("Test", "Test", g_ScriptRuntime);
JSContext* cx = script.GetContext();
JSAutoRequest rq(cx);
ScriptInterface::Request rq(script);
JS::RootedValue obj(cx);
JS::RootedValue obj(rq.cx);
TSM_ASSERT(msg, script.Eval(input, &obj));
if (debug)
@@ -318,7 +317,7 @@ public:
CStdDeserializer deserialize(script, stream);
JS::RootedValue newobj(cx);
JS::RootedValue newobj(rq.cx);
deserialize.ScriptVal("script", &newobj);
// NOTE: Don't use good() here - it fails due to a bug in older libc++ versions
TSM_ASSERT(msg, !stream.bad() && !stream.fail());
@@ -755,10 +754,9 @@ public:
void test_script_exceptions()
{
ScriptInterface script("Test", "Test", g_ScriptRuntime);
JSContext* cx = script.GetContext();
JSAutoRequest rq(cx);
ScriptInterface::Request rq(script);
JS::RootedValue obj(cx);
JS::RootedValue obj(rq.cx);
std::stringstream stream;
CStdSerializer serialize(script, stream);
@@ -790,10 +788,9 @@ public:
const char* input = "var x = {}; for (var i=0;i<256;++i) x[i]=Math.pow(i, 2); x";
ScriptInterface script("Test", "Test", g_ScriptRuntime);
JSContext* cx = script.GetContext();
JSAutoRequest rq(cx);
ScriptInterface::Request rq(script);
JS::RootedValue obj(cx);
JS::RootedValue obj(rq.cx);
TS_ASSERT(script.Eval(input, &obj));
for (size_t i = 0; i < 256; ++i)
@@ -805,7 +802,7 @@ public:
CStdDeserializer deserialize(script, stream);
JS::RootedValue newobj(cx);
JS::RootedValue newobj(rq.cx);
deserialize.ScriptVal("script", &newobj);
// NOTE: Don't use good() here - it fails due to a bug in older libc++ versions
TS_ASSERT(!stream.bad() && !stream.fail());