]> Cypherpunks repositories - gostls13.git/commitdiff
syscall/js: move callback helper code to misc/wasm to avoid using eval()
authorXudong Zheng <7pkvm5aw@slicealias.com>
Wed, 1 Aug 2018 21:33:09 +0000 (21:33 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 1 Aug 2018 21:45:11 +0000 (21:45 +0000)
When using the compiled .wasm with misc/wasm/wasm_exec.js, we get an error message if the site prohibits eval() via the Content-Security-Policy header. This can be resolved by moving the callback helper code from src/syscall/js/callback.go to misc/wasm/wasm_exec.js.

Fixes #26748

Change-Id: I28f271b8a00631f4c66a1ac31305e85f20f9d420
GitHub-Last-Rev: a6a0268f38d36c198ca6b4ceb2e75cc8afec74eb
GitHub-Pull-Request: golang/go#26750
Reviewed-on: https://go-review.googlesource.com/127296
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
misc/wasm/wasm_exec.js
src/syscall/js/callback.go

index f3772652da3f757011f5bd0141a9597fcac6a51a..94b9552c59606cce2e3107808022f088916b0b0a 100644 (file)
                                await callbackPromise;
                        }
                }
+
+               static _makeCallbackHelper(id, pendingCallbacks, go) {
+                       return function() {
+                               pendingCallbacks.push({ id: id, args: arguments });
+                               go._resolveCallbackPromise();
+                       };
+               }
+
+               static _makeEventCallbackHelper(preventDefault, stopPropagation, stopImmediatePropagation, fn) {
+                       return function(event) {
+                               if (preventDefault) {
+                                       event.preventDefault();
+                               }
+                               if (stopPropagation) {
+                                       event.stopPropagation();
+                               }
+                               if (stopImmediatePropagation) {
+                                       event.stopImmediatePropagation();
+                               }
+                               fn(event);
+                       };
+               }
        }
 
        if (isNodeJS) {
index 346669ad34464d65b208d655f90caf612f3b3154..9d573074cbd3990de97c4f13bc368dd7a2640863 100644 (file)
@@ -8,33 +8,11 @@ package js
 
 import "sync"
 
-var pendingCallbacks = Global().Get("Array").New()
-
-var makeCallbackHelper = Global().Call("eval", `
-       (function(id, pendingCallbacks, go) {
-               return function() {
-                       pendingCallbacks.push({ id: id, args: arguments });
-                       go._resolveCallbackPromise();
-               };
-       })
-`)
-
-var makeEventCallbackHelper = Global().Call("eval", `
-       (function(preventDefault, stopPropagation, stopImmediatePropagation, fn) {
-               return function(event) {
-                       if (preventDefault) {
-                               event.preventDefault();
-                       }
-                       if (stopPropagation) {
-                               event.stopPropagation();
-                       }
-                       if (stopImmediatePropagation) {
-                               event.stopImmediatePropagation();
-                       }
-                       fn(event);
-               };
-       })
-`)
+var (
+       pendingCallbacks        = Global().Get("Array").New()
+       makeCallbackHelper      = Global().Get("Go").Get("_makeCallbackHelper")
+       makeEventCallbackHelper = Global().Get("Go").Get("_makeEventCallbackHelper")
+)
 
 var (
        callbacksMu    sync.Mutex