summaryrefslogtreecommitdiff
path: root/fxjs/cfxjs_engine_embeddertest.cpp
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2018-05-17 13:53:52 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-05-17 13:53:52 +0000
commitdc5d88bcebbeeb696b405464e901add55d1efaf7 (patch)
tree54bc913015ea4d08b9fe46997f25a3153cbc70d7 /fxjs/cfxjs_engine_embeddertest.cpp
parentdb3c6cefceddf25c25f1205d7b633f09e873bf98 (diff)
downloadpdfium-dc5d88bcebbeeb696b405464e901add55d1efaf7.tar.xz
Convert JS execute methods to return Optional<IJS_Runtime::JS_Error>
This CL changes several of the JS execution methods to to return an Optional<IJS_Runtime::JS_Error> instead of a bool with a WideString out param. The IJS_Runtime::JS_Error will contain the line, column and exception message if an error occurs during execution. Change-Id: I37785ae6cd133a4c94ad8d25289473600b8a5d19 Reviewed-on: https://pdfium-review.googlesource.com/32614 Commit-Queue: dsinclair <dsinclair@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'fxjs/cfxjs_engine_embeddertest.cpp')
-rw-r--r--fxjs/cfxjs_engine_embeddertest.cpp51
1 files changed, 43 insertions, 8 deletions
diff --git a/fxjs/cfxjs_engine_embeddertest.cpp b/fxjs/cfxjs_engine_embeddertest.cpp
index bcd4183de0..75f07982aa 100644
--- a/fxjs/cfxjs_engine_embeddertest.cpp
+++ b/fxjs/cfxjs_engine_embeddertest.cpp
@@ -21,13 +21,13 @@ const wchar_t kScript2[] = L"fred = 8";
class CFXJSEngineEmbedderTest : public JSEmbedderTest {
public:
- void ExecuteInCurrentContext(const WideString& script) {
+ Optional<IJS_Runtime::JS_Error> ExecuteInCurrentContext(
+ const WideString& script) {
auto* current_engine =
CFXJS_Engine::EngineFromIsolateCurrentContext(isolate());
- FXJSErr error;
- int sts = current_engine->Execute(script, &error);
- EXPECT_EQ(0, sts);
+ return current_engine->Execute(script);
}
+
void CheckAssignmentInCurrentContext(double expected) {
auto* current_engine =
CFXJS_Engine::EngineFromIsolateCurrentContext(isolate());
@@ -44,7 +44,9 @@ TEST_F(CFXJSEngineEmbedderTest, Getters) {
v8::HandleScope handle_scope(isolate());
v8::Context::Scope context_scope(GetV8Context());
- ExecuteInCurrentContext(WideString(kScript1));
+ Optional<IJS_Runtime::JS_Error> err =
+ ExecuteInCurrentContext(WideString(kScript1));
+ EXPECT_FALSE(err);
CheckAssignmentInCurrentContext(kExpected1);
}
@@ -62,17 +64,23 @@ TEST_F(CFXJSEngineEmbedderTest, MultipleEngines) {
v8::Local<v8::Context> context2 = engine2.GetV8Context();
v8::Context::Scope context_scope(GetV8Context());
- ExecuteInCurrentContext(WideString(kScript0));
+ Optional<IJS_Runtime::JS_Error> err =
+ ExecuteInCurrentContext(WideString(kScript0));
+ EXPECT_FALSE(err);
CheckAssignmentInCurrentContext(kExpected0);
{
v8::Context::Scope context_scope1(context1);
- ExecuteInCurrentContext(WideString(kScript1));
+ Optional<IJS_Runtime::JS_Error> err =
+ ExecuteInCurrentContext(WideString(kScript1));
+ EXPECT_FALSE(err);
CheckAssignmentInCurrentContext(kExpected1);
}
{
v8::Context::Scope context_scope2(context2);
- ExecuteInCurrentContext(WideString(kScript2));
+ Optional<IJS_Runtime::JS_Error> err =
+ ExecuteInCurrentContext(WideString(kScript2));
+ EXPECT_FALSE(err);
CheckAssignmentInCurrentContext(kExpected2);
}
@@ -102,3 +110,30 @@ TEST_F(CFXJSEngineEmbedderTest, MultipleEngines) {
engine1.ReleaseEngine();
engine2.ReleaseEngine();
}
+
+TEST_F(CFXJSEngineEmbedderTest, JSCompileError) {
+ v8::Isolate::Scope isolate_scope(isolate());
+ v8::HandleScope handle_scope(isolate());
+ v8::Context::Scope context_scope(GetV8Context());
+
+ Optional<IJS_Runtime::JS_Error> err =
+ ExecuteInCurrentContext(L"functoon(x) { return x+1; }");
+ EXPECT_TRUE(err);
+ EXPECT_EQ(L"SyntaxError: Unexpected token {", err->exception);
+ EXPECT_EQ(1, err->line);
+ EXPECT_EQ(12, err->column);
+}
+
+TEST_F(CFXJSEngineEmbedderTest, JSRuntimeError) {
+ v8::Isolate::Scope isolate_scope(isolate());
+ v8::HandleScope handle_scope(isolate());
+ v8::Context::Scope context_scope(GetV8Context());
+
+ Optional<IJS_Runtime::JS_Error> err =
+ ExecuteInCurrentContext(L"let a = 3;\nundefined.colour");
+ EXPECT_TRUE(err);
+ EXPECT_EQ(L"TypeError: Cannot read property 'colour' of undefined",
+ err->exception);
+ EXPECT_EQ(2, err->line);
+ EXPECT_EQ(10, err->column);
+}