From 9c3c5c10465f18f1082ce1bcfe5393eb98ef053c Mon Sep 17 00:00:00 2001 From: Leo Ufimtsev Date: Fri, 29 Sep 2017 11:57:53 -0400 Subject: Bug 525340 [Gtk][Webkit2] Javascript evaluate fails with "SyntaxError: Return statements are only valid inside functions" Wrapping evaluate logic into a function to deal with corner case where 'return' is not at the beginnign of the script. Tested with all Browser jUnit tests. Change-Id: Icb7c6d29006a0382fb5525bd7184101c3ea0cbdd Signed-off-by: Leo Ufimtsev --- .../gtk/org/eclipse/swt/browser/WebKit.java | 26 ++++++++++------------ 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/eclipse.platform.swt/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java b/eclipse.platform.swt/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java index f9361e1..1227298 100644 --- a/eclipse.platform.swt/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java +++ b/eclipse.platform.swt/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java @@ -1216,7 +1216,7 @@ // Mechanism to generate unique ID's private static int nextCallbackId = 1; private static HashSet usedCallbackIds = new HashSet<>(); - private static int getNextId() { + static int getNextId() { int value = 0; boolean unique = false; while (unique == false) { @@ -1236,28 +1236,26 @@ } static Object evaluate(String script, Browser browser, long /*int*/ webView, boolean doNotBlock) { - /* Webkit2: We remove the 'return' prefix that normally comes with the script. - * The reason is that in Webkit1, script was wrapped into a function and if an exception occured - * it was caught on Javascript side and a callback to java was made. - * In Webkit2, we handle errors in the callback, no need to wrap them in a function anymore. + /* Wrap script around a function for backwards compatibility, + * user can specify 'return', which may not be at the beginning of the script. + * Valid scripts: + * 'hi' + * return 'hi' + * var x = 1; return 'hi' */ - String fixedScript; - if (script.length() > 7 && script.substring(0, 7).equals("return ")) { - fixedScript = script.substring(7); - } else { - fixedScript = script; - } + String swtUniqueExecFunc = "SWTWebkit2TempFunc" + CallBackMap.getNextId() + "()"; + String wrappedScript = "function " + swtUniqueExecFunc +"{" + script + "}; " + swtUniqueExecFunc; if (doNotBlock) { // Execute script, but do not wait for async call to complete. (assume it does). Bug 512001. - WebKitGTK.webkit_web_view_run_javascript(webView, Converter.wcsToMbcs(fixedScript, true), 0, 0, 0); + WebKitGTK.webkit_web_view_run_javascript(webView, Converter.wcsToMbcs(wrappedScript, true), 0, 0, 0); return null; } else { // Callback logic: Initiate an async callback and wait for it to finish. // The callback comes back in javascriptExecutionFinishedProc(..) below. Webkit2EvalReturnObj retObj = new Webkit2EvalReturnObj(); int callbackId = CallBackMap.putObject(retObj); - WebKitGTK.webkit_web_view_run_javascript(webView, Converter.wcsToMbcs(fixedScript, true), 0, callback.getAddress(), callbackId); + WebKitGTK.webkit_web_view_run_javascript(webView, Converter.wcsToMbcs(wrappedScript, true), 0, callback.getAddress(), callbackId); Shell shell = browser.getShell(); Display display = browser.getDisplay(); while (!shell.isDisposed()) { @@ -1270,7 +1268,7 @@ CallBackMap.removeObject(callbackId); if (retObj.errorNum != 0) { - throw new SWTException(retObj.errorNum, retObj.errorMsg); + throw new SWTException(retObj.errorNum, retObj.errorMsg +"\nScript that was evaluated:\n" + wrappedScript); } else { return retObj.returnValue; } -- cgit v1.1