Skip to content

syscall/js: move callback helper code to misc/wasm to avoid using eval() #26750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions misc/wasm/wasm_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,28 @@
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) {
Expand Down
32 changes: 5 additions & 27 deletions src/syscall/js/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down