Skip to content

Commit a6a0268

Browse files
committed
syscall/js: move callback helper code to misc/wasm to avoid using eval()
1 parent b8669ef commit a6a0268

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

misc/wasm/wasm_exec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,28 @@
387387
await callbackPromise;
388388
}
389389
}
390+
391+
static _makeCallbackHelper(id, pendingCallbacks, go) {
392+
return function() {
393+
pendingCallbacks.push({ id: id, args: arguments });
394+
go._resolveCallbackPromise();
395+
};
396+
}
397+
398+
static _makeEventCallbackHelper(preventDefault, stopPropagation, stopImmediatePropagation, fn) {
399+
return function(event) {
400+
if (preventDefault) {
401+
event.preventDefault();
402+
}
403+
if (stopPropagation) {
404+
event.stopPropagation();
405+
}
406+
if (stopImmediatePropagation) {
407+
event.stopImmediatePropagation();
408+
}
409+
fn(event);
410+
};
411+
}
390412
}
391413

392414
if (isNodeJS) {

src/syscall/js/callback.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,11 @@ package js
88

99
import "sync"
1010

11-
var pendingCallbacks = Global().Get("Array").New()
12-
13-
var makeCallbackHelper = Global().Call("eval", `
14-
(function(id, pendingCallbacks, go) {
15-
return function() {
16-
pendingCallbacks.push({ id: id, args: arguments });
17-
go._resolveCallbackPromise();
18-
};
19-
})
20-
`)
21-
22-
var makeEventCallbackHelper = Global().Call("eval", `
23-
(function(preventDefault, stopPropagation, stopImmediatePropagation, fn) {
24-
return function(event) {
25-
if (preventDefault) {
26-
event.preventDefault();
27-
}
28-
if (stopPropagation) {
29-
event.stopPropagation();
30-
}
31-
if (stopImmediatePropagation) {
32-
event.stopImmediatePropagation();
33-
}
34-
fn(event);
35-
};
36-
})
37-
`)
11+
var (
12+
pendingCallbacks = Global().Get("Array").New()
13+
makeCallbackHelper = Global().Get("Go").Get("_makeCallbackHelper")
14+
makeEventCallbackHelper = Global().Get("Go").Get("_makeEventCallbackHelper")
15+
)
3816

3917
var (
4018
callbacksMu sync.Mutex

0 commit comments

Comments
 (0)