diff options
-rw-r--r-- | fxjs/cfxjs_engine_embeddertest.cpp | 89 |
1 files changed, 27 insertions, 62 deletions
diff --git a/fxjs/cfxjs_engine_embeddertest.cpp b/fxjs/cfxjs_engine_embeddertest.cpp index 75f07982aa..ff866d3ee0 100644 --- a/fxjs/cfxjs_engine_embeddertest.cpp +++ b/fxjs/cfxjs_engine_embeddertest.cpp @@ -19,35 +19,25 @@ const wchar_t kScript2[] = L"fred = 8"; } // namespace -class CFXJSEngineEmbedderTest : public JSEmbedderTest { - public: - Optional<IJS_Runtime::JS_Error> ExecuteInCurrentContext( - const WideString& script) { - auto* current_engine = - CFXJS_Engine::EngineFromIsolateCurrentContext(isolate()); - return current_engine->Execute(script); - } - - void CheckAssignmentInCurrentContext(double expected) { - auto* current_engine = - CFXJS_Engine::EngineFromIsolateCurrentContext(isolate()); - v8::Local<v8::Object> This = current_engine->GetThisObj(); - v8::Local<v8::Value> fred = - current_engine->GetObjectProperty(This, L"fred"); - EXPECT_TRUE(fred->IsNumber()); - EXPECT_EQ(expected, current_engine->ToDouble(fred)); - } -}; +using CFXJSEngineEmbedderTest = JSEmbedderTest; + +void CheckAssignmentInEngineContext(CFXJS_Engine* current_engine, + double expected) { + v8::Context::Scope context_scope(current_engine->GetV8Context()); + v8::Local<v8::Object> This = current_engine->GetThisObj(); + v8::Local<v8::Value> fred = current_engine->GetObjectProperty(This, L"fred"); + EXPECT_TRUE(fred->IsNumber()); + EXPECT_EQ(expected, current_engine->ToDouble(fred)); +} TEST_F(CFXJSEngineEmbedderTest, Getters) { v8::Isolate::Scope isolate_scope(isolate()); v8::HandleScope handle_scope(isolate()); v8::Context::Scope context_scope(GetV8Context()); - Optional<IJS_Runtime::JS_Error> err = - ExecuteInCurrentContext(WideString(kScript1)); + Optional<IJS_Runtime::JS_Error> err = engine()->Execute(WideString(kScript1)); EXPECT_FALSE(err); - CheckAssignmentInCurrentContext(kExpected1); + CheckAssignmentInEngineContext(engine(), kExpected1); } TEST_F(CFXJSEngineEmbedderTest, MultipleEngines) { @@ -60,53 +50,28 @@ TEST_F(CFXJSEngineEmbedderTest, MultipleEngines) { CFXJS_Engine engine2(isolate()); engine2.InitializeEngine(); - v8::Local<v8::Context> context1 = engine1.GetV8Context(); - v8::Local<v8::Context> context2 = engine2.GetV8Context(); - v8::Context::Scope context_scope(GetV8Context()); - Optional<IJS_Runtime::JS_Error> err = - ExecuteInCurrentContext(WideString(kScript0)); + Optional<IJS_Runtime::JS_Error> err = engine()->Execute(WideString(kScript0)); EXPECT_FALSE(err); - CheckAssignmentInCurrentContext(kExpected0); + CheckAssignmentInEngineContext(engine(), kExpected0); { - v8::Context::Scope context_scope1(context1); - Optional<IJS_Runtime::JS_Error> err = - ExecuteInCurrentContext(WideString(kScript1)); + // engine1 executing in engine1's context doesn't affect main. + v8::Context::Scope context_scope1(engine1.GetV8Context()); + Optional<IJS_Runtime::JS_Error> err = engine1.Execute(WideString(kScript1)); EXPECT_FALSE(err); - CheckAssignmentInCurrentContext(kExpected1); + CheckAssignmentInEngineContext(engine(), kExpected0); + CheckAssignmentInEngineContext(&engine1, kExpected1); } { - v8::Context::Scope context_scope2(context2); - Optional<IJS_Runtime::JS_Error> err = - ExecuteInCurrentContext(WideString(kScript2)); + // engine1 executing in engine2's context doesn't affect engine1. + v8::Context::Scope context_scope2(engine2.GetV8Context()); + Optional<IJS_Runtime::JS_Error> err = engine1.Execute(WideString(kScript2)); EXPECT_FALSE(err); - CheckAssignmentInCurrentContext(kExpected2); + CheckAssignmentInEngineContext(engine(), kExpected0); + CheckAssignmentInEngineContext(&engine1, kExpected1); + CheckAssignmentInEngineContext(&engine2, kExpected2); } - - CheckAssignmentInCurrentContext(kExpected0); - - { - v8::Context::Scope context_scope1(context1); - CheckAssignmentInCurrentContext(kExpected1); - { - v8::Context::Scope context_scope2(context2); - CheckAssignmentInCurrentContext(kExpected2); - } - CheckAssignmentInCurrentContext(kExpected1); - } - { - v8::Context::Scope context_scope2(context2); - CheckAssignmentInCurrentContext(kExpected2); - { - v8::Context::Scope context_scope1(context1); - CheckAssignmentInCurrentContext(kExpected1); - } - CheckAssignmentInCurrentContext(kExpected2); - } - - CheckAssignmentInCurrentContext(kExpected0); - engine1.ReleaseEngine(); engine2.ReleaseEngine(); } @@ -117,7 +82,7 @@ TEST_F(CFXJSEngineEmbedderTest, JSCompileError) { v8::Context::Scope context_scope(GetV8Context()); Optional<IJS_Runtime::JS_Error> err = - ExecuteInCurrentContext(L"functoon(x) { return x+1; }"); + engine()->Execute(L"functoon(x) { return x+1; }"); EXPECT_TRUE(err); EXPECT_EQ(L"SyntaxError: Unexpected token {", err->exception); EXPECT_EQ(1, err->line); @@ -130,7 +95,7 @@ TEST_F(CFXJSEngineEmbedderTest, JSRuntimeError) { v8::Context::Scope context_scope(GetV8Context()); Optional<IJS_Runtime::JS_Error> err = - ExecuteInCurrentContext(L"let a = 3;\nundefined.colour"); + engine()->Execute(L"let a = 3;\nundefined.colour"); EXPECT_TRUE(err); EXPECT_EQ(L"TypeError: Cannot read property 'colour' of undefined", err->exception); |