@primate/go 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/package.json +51 -0
- package/src/default.js +8 -0
- package/src/env.js +1 -0
- package/src/private/build.js +172 -0
- package/src/private/error/faulty-route.js +10 -0
- package/src/private/extension.js +1 -0
- package/src/private/go.mod +5 -0
- package/src/private/go.sum +2 -0
- package/src/private/pkgname.js +1 -0
- package/src/private/request.go +117 -0
- package/src/private/session.go +70 -0
- package/src/runtime.js +3 -0
- package/src/to-request.js +54 -0
- package/src/to-response.js +23 -0
- package/src/vendored/wasm_exec.js +559 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) Terrablue <terrablue@proton.me> and contributors.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
16
|
+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@primate/go",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Primate Go backend",
|
|
5
|
+
"homepage": "https://primatejs.com/modules/go",
|
|
6
|
+
"bugs": "https://github.com/primatejs/primate/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"files": [
|
|
9
|
+
"src/**/*.js",
|
|
10
|
+
"!src/**/*.spec.js",
|
|
11
|
+
"src/**/*.go",
|
|
12
|
+
"src/**/*.sum",
|
|
13
|
+
"src/**/*.mod"
|
|
14
|
+
],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/primatejs/primate",
|
|
18
|
+
"directory": "packages/go"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@rcompat/cli": "^0.5.1",
|
|
22
|
+
"@rcompat/env": "^0.3.0",
|
|
23
|
+
"@rcompat/fs": "^0.4.0",
|
|
24
|
+
"@rcompat/invariant": "^0.5.0",
|
|
25
|
+
"@rcompat/object": "^0.5.0",
|
|
26
|
+
"@rcompat/platform": "^0.3.0",
|
|
27
|
+
"@rcompat/stdio": "^0.4.0",
|
|
28
|
+
"@primate/core": "^0.1.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"primate": "^0.32.0"
|
|
32
|
+
},
|
|
33
|
+
"type": "module",
|
|
34
|
+
"imports": {
|
|
35
|
+
"#*": {
|
|
36
|
+
"@primate/lt": "./src/private/*.js",
|
|
37
|
+
"default": "./src/private/*.js"
|
|
38
|
+
},
|
|
39
|
+
"#error/*": {
|
|
40
|
+
"@primate/lt": "./src/private/error/*.js",
|
|
41
|
+
"default": "./src/private/error/*.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"exports": {
|
|
45
|
+
".": {
|
|
46
|
+
"runtime": "./src/runtime.js",
|
|
47
|
+
"default": "./src/default.js"
|
|
48
|
+
},
|
|
49
|
+
"./*": "./src/*.js"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/default.js
ADDED
package/src/env.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./vendored/wasm_exec.js";
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import faulty_route from "#error/faulty-route";
|
|
2
|
+
import pkgname from "#pkgname";
|
|
3
|
+
import log from "@primate/core/log";
|
|
4
|
+
import dim from "@rcompat/cli/color/dim";
|
|
5
|
+
import { user } from "@rcompat/env";
|
|
6
|
+
import file from "@rcompat/fs/file";
|
|
7
|
+
import platform from "@rcompat/platform";
|
|
8
|
+
import execute from "@rcompat/stdio/execute";
|
|
9
|
+
|
|
10
|
+
const command = "go";
|
|
11
|
+
const run = (wasm, go, includes = "request.go") =>
|
|
12
|
+
`${command} build -o ${wasm} ${go} ${includes}`;
|
|
13
|
+
const routes_re = /func (?<route>Get|Post|Put|Delete)/gu;
|
|
14
|
+
const add_setter = route => `
|
|
15
|
+
var cb${route} js.Func;
|
|
16
|
+
cb${route} = js.FuncOf(func(this js.Value, args[]js.Value) any {
|
|
17
|
+
cb${route}.Release();
|
|
18
|
+
return make_request(${route}, args[0]);
|
|
19
|
+
});
|
|
20
|
+
js.Global().Set("${route}", cb${route});
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
const make_route = route =>
|
|
24
|
+
` ${route.toLowerCase()}(request) {
|
|
25
|
+
const go = new globalThis.Go();
|
|
26
|
+
return WebAssembly.instantiate(route, {...go.importObject}).then(result => {
|
|
27
|
+
go.run(result.instance);
|
|
28
|
+
return to_response(globalThis.${route}(to_request(request)));
|
|
29
|
+
});
|
|
30
|
+
}`;
|
|
31
|
+
|
|
32
|
+
const js_wrapper = (path, routes) => `
|
|
33
|
+
import env from "@primate/go/env";
|
|
34
|
+
import to_request from "@primate/go/to-request";
|
|
35
|
+
import to_response from "@primate/go/to-response";
|
|
36
|
+
${
|
|
37
|
+
platform === "bun" ? `
|
|
38
|
+
import route_path from "${path}" with { type: "file" };
|
|
39
|
+
const route = await Bun.file(route_path).arrayBuffer();
|
|
40
|
+
` : `
|
|
41
|
+
import file from "@rcompat/fs/file";
|
|
42
|
+
const route = new Uint8Array(await file(import.meta.dirname+"/${path}")
|
|
43
|
+
.arrayBuffer());
|
|
44
|
+
`
|
|
45
|
+
}
|
|
46
|
+
env();
|
|
47
|
+
|
|
48
|
+
export default {
|
|
49
|
+
${routes.map(route => make_route(route)).join(",\n")}
|
|
50
|
+
};`;
|
|
51
|
+
|
|
52
|
+
const go_wrapper = (code, routes) =>
|
|
53
|
+
`// {{{ wrapper prefix
|
|
54
|
+
package main
|
|
55
|
+
import "syscall/js"
|
|
56
|
+
// }}} end
|
|
57
|
+
${code}
|
|
58
|
+
// {{{ wrapper postfix
|
|
59
|
+
func main() {
|
|
60
|
+
${routes.map(route => add_setter(route)).join("\n ")}
|
|
61
|
+
select{};
|
|
62
|
+
}
|
|
63
|
+
// }}} end`;
|
|
64
|
+
|
|
65
|
+
const get_routes = code => [...code.matchAll(routes_re)]
|
|
66
|
+
.map(({ groups: { route } }) => route);
|
|
67
|
+
|
|
68
|
+
const type_map = {
|
|
69
|
+
boolean: { transfer: "Bool", type: "bool" },
|
|
70
|
+
i8: { transfer: "Int", type: "int8" },
|
|
71
|
+
i16: { transfer: "Int", type: "int16" },
|
|
72
|
+
i32: { transfer: "Int", type: "int32" },
|
|
73
|
+
i64: { transfer: "Int", type: "int64" },
|
|
74
|
+
f32: { transfer: "Float", type: "float32" },
|
|
75
|
+
f64: { transfer: "Float", type: "float64" },
|
|
76
|
+
u8: { transfer: "Int", type: "uint8" },
|
|
77
|
+
u16: { transfer: "Int", type: "uint16" },
|
|
78
|
+
u32: { transfer: "Int", type: "uint32", nullval: "0" },
|
|
79
|
+
u64: { transfer: "Int", type: "uint64" },
|
|
80
|
+
string: { transfer: "String", type: "string" },
|
|
81
|
+
uuid: { transfer: "String", type: "string" },
|
|
82
|
+
};
|
|
83
|
+
const error_default = {
|
|
84
|
+
Bool: false,
|
|
85
|
+
Int: 0,
|
|
86
|
+
Float: 0,
|
|
87
|
+
String: "\"\"",
|
|
88
|
+
};
|
|
89
|
+
const root = file(import.meta.url).up(1);
|
|
90
|
+
|
|
91
|
+
const create_meta_files = async (directory, app) => {
|
|
92
|
+
const meta = {
|
|
93
|
+
mod: "go.mod",
|
|
94
|
+
sum: "go.sum",
|
|
95
|
+
request: "request.go",
|
|
96
|
+
session: "session.go",
|
|
97
|
+
};
|
|
98
|
+
const has_session = app.modules.names.includes("@primate/session");
|
|
99
|
+
|
|
100
|
+
if (!await directory.join(meta.mod).exists()) {
|
|
101
|
+
const request_struct_items = [];
|
|
102
|
+
const request_make_items = [];
|
|
103
|
+
|
|
104
|
+
if (has_session) {
|
|
105
|
+
request_struct_items.push(
|
|
106
|
+
"Session Session",
|
|
107
|
+
);
|
|
108
|
+
request_make_items.push(
|
|
109
|
+
"make_session(request),",
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const request_struct = request_struct_items.join("\n");
|
|
114
|
+
const request_make = request_make_items.join("\n");
|
|
115
|
+
|
|
116
|
+
// copy go.mod file
|
|
117
|
+
await directory.join(meta.mod).write(await root.join(meta.mod).text());
|
|
118
|
+
// copy go.sum file
|
|
119
|
+
await directory.join(meta.sum).write(await root.join(meta.sum).text());
|
|
120
|
+
|
|
121
|
+
// copy transformed request.go file
|
|
122
|
+
await directory.join(meta.request).write((await root.join(meta.request)
|
|
123
|
+
.text())
|
|
124
|
+
.replace("%%REQUEST_STRUCT%%", _ => request_struct)
|
|
125
|
+
.replace("%%REQUEST_MAKE%%", _ => request_make),
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
if (has_session) {
|
|
129
|
+
// copy session.go file
|
|
130
|
+
directory.join(meta.session).write(await root.join(meta.session).text());
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return ["request.go"]
|
|
135
|
+
.concat(has_session ? ["session.go"] : [])
|
|
136
|
+
;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const env = { GOOS: "js", GOARCH: "wasm" };
|
|
140
|
+
|
|
141
|
+
export default ({ extension }) => (app, next) => {
|
|
142
|
+
app.bind(extension, async (directory, file) => {
|
|
143
|
+
const path = directory.join(file);
|
|
144
|
+
const base = path.directory;
|
|
145
|
+
const go = path.base.concat(".go");
|
|
146
|
+
const wasm = path.base.concat(".wasm");
|
|
147
|
+
const js = path.base.concat(".js");
|
|
148
|
+
|
|
149
|
+
// create meta files
|
|
150
|
+
const includes = await create_meta_files(base, app);
|
|
151
|
+
|
|
152
|
+
const code = await path.text();
|
|
153
|
+
const routes = get_routes(code);
|
|
154
|
+
// write .go file
|
|
155
|
+
await path.write(go_wrapper(code, routes));
|
|
156
|
+
// write .js wrapper
|
|
157
|
+
const wasm_route_path = `./${file.name.slice(0, -extension.length)}.wasm`;
|
|
158
|
+
await base.join(js).write(js_wrapper(wasm_route_path, routes));
|
|
159
|
+
|
|
160
|
+
try {
|
|
161
|
+
log.info(`compiling ${dim(file)} to WebAssembly`, { module: pkgname });
|
|
162
|
+
const cwd = `${base}`;
|
|
163
|
+
// compile .go to .wasm
|
|
164
|
+
await execute(run(wasm, go, includes.join(" ")),
|
|
165
|
+
{ cwd, env: { HOME: user.HOME, ...env } });
|
|
166
|
+
} catch (error) {
|
|
167
|
+
faulty_route(file, error);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
return next(app);
|
|
172
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import pkgname from "#pkgname";
|
|
2
|
+
import log from "@primate/core/log";
|
|
3
|
+
import file from "@rcompat/fs/file";
|
|
4
|
+
|
|
5
|
+
export default (...params) => log.error({
|
|
6
|
+
params,
|
|
7
|
+
name: file(import.meta.url).base,
|
|
8
|
+
module: pkgname,
|
|
9
|
+
message: "the Go route {0} contains the following error\n {1}",
|
|
10
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default ".go";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default "@primate/go";
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import "syscall/js"
|
|
4
|
+
import "encoding/json"
|
|
5
|
+
|
|
6
|
+
type t_request func(Request) any
|
|
7
|
+
type t_response func(js.Value, []js.Value) any
|
|
8
|
+
type Object map[string]any
|
|
9
|
+
type Array []any
|
|
10
|
+
|
|
11
|
+
type Dispatcher struct {
|
|
12
|
+
Get func(string) any
|
|
13
|
+
Json func() map[string]any
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type URL struct {
|
|
17
|
+
Href string
|
|
18
|
+
Origin string
|
|
19
|
+
Protocol string
|
|
20
|
+
Username string
|
|
21
|
+
Password string
|
|
22
|
+
Host string
|
|
23
|
+
Hostname string
|
|
24
|
+
Port string
|
|
25
|
+
Pathname string
|
|
26
|
+
Search string
|
|
27
|
+
SearchParams map[string]any
|
|
28
|
+
Hash string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type Request struct {
|
|
32
|
+
Url URL
|
|
33
|
+
Body map[string]any
|
|
34
|
+
Path Dispatcher
|
|
35
|
+
Query Dispatcher
|
|
36
|
+
Cookies Dispatcher
|
|
37
|
+
Headers Dispatcher
|
|
38
|
+
%%REQUEST_STRUCT%%
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
func make_url(request js.Value) URL {
|
|
42
|
+
url := request.Get("url");
|
|
43
|
+
search_params := make(map[string]any);
|
|
44
|
+
json.Unmarshal([]byte(request.Get("search_params").String()), &search_params);
|
|
45
|
+
|
|
46
|
+
return URL{
|
|
47
|
+
url.Get("href").String(),
|
|
48
|
+
url.Get("origin").String(),
|
|
49
|
+
url.Get("protocol").String(),
|
|
50
|
+
url.Get("username").String(),
|
|
51
|
+
url.Get("password").String(),
|
|
52
|
+
url.Get("host").String(),
|
|
53
|
+
url.Get("hostname").String(),
|
|
54
|
+
url.Get("port").String(),
|
|
55
|
+
url.Get("pathname").String(),
|
|
56
|
+
url.Get("search").String(),
|
|
57
|
+
search_params,
|
|
58
|
+
url.Get("hash").String(),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
func make_dispatcher(key string, request js.Value) Dispatcher {
|
|
63
|
+
properties := make(map[string]any);
|
|
64
|
+
value := request.Get(key)
|
|
65
|
+
json.Unmarshal([]byte(value.Get("stringified").String()), &properties);
|
|
66
|
+
|
|
67
|
+
return Dispatcher{
|
|
68
|
+
// Get
|
|
69
|
+
func(property string) any {
|
|
70
|
+
switch properties[property].(type) {
|
|
71
|
+
case string:
|
|
72
|
+
return properties[property].(string);
|
|
73
|
+
default:
|
|
74
|
+
return nil;
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
// Json
|
|
78
|
+
func() map[string]any {
|
|
79
|
+
return properties;
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
func make_request(route t_request, request js.Value) any {
|
|
85
|
+
body := make(map[string]any);
|
|
86
|
+
json.Unmarshal([]byte(request.Get("body").String()), &body);
|
|
87
|
+
|
|
88
|
+
go_request := Request{
|
|
89
|
+
make_url(request),
|
|
90
|
+
body,
|
|
91
|
+
make_dispatcher("path", request),
|
|
92
|
+
make_dispatcher("query", request),
|
|
93
|
+
make_dispatcher("cookies", request),
|
|
94
|
+
make_dispatcher("headers", request),
|
|
95
|
+
%%REQUEST_MAKE%%
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
response := route(go_request);
|
|
99
|
+
switch response.(type) {
|
|
100
|
+
case js.Func:
|
|
101
|
+
return response;
|
|
102
|
+
default:
|
|
103
|
+
marshalled, _ := json.Marshal(response);
|
|
104
|
+
return string(marshalled);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
func make_empty(route func() any) any {
|
|
109
|
+
response := route();
|
|
110
|
+
switch response.(type) {
|
|
111
|
+
case js.Func:
|
|
112
|
+
return response;
|
|
113
|
+
default:
|
|
114
|
+
marshalled, _ := json.Marshal(response);
|
|
115
|
+
return string(marshalled);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import "syscall/js"
|
|
4
|
+
import "encoding/json"
|
|
5
|
+
import "errors"
|
|
6
|
+
|
|
7
|
+
type Session struct {
|
|
8
|
+
Exists func() bool
|
|
9
|
+
Get func(string) any
|
|
10
|
+
Json func() map[string]any
|
|
11
|
+
Set func(string, any) error
|
|
12
|
+
Create func(map[string]any)
|
|
13
|
+
Destroy func()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
func make_session(request js.Value) Session {
|
|
17
|
+
session := request.Get("session");
|
|
18
|
+
properties := make(map[string]any);
|
|
19
|
+
json.Unmarshal([]byte(session.Get("stringified").String()), &properties);
|
|
20
|
+
|
|
21
|
+
return Session{
|
|
22
|
+
// Exists
|
|
23
|
+
func() bool {
|
|
24
|
+
return session.Get("exists").Invoke().Bool();
|
|
25
|
+
},
|
|
26
|
+
// Get
|
|
27
|
+
func(key string) any {
|
|
28
|
+
invoked := session.Get("get").Invoke(key);
|
|
29
|
+
jstype := invoked.Type()
|
|
30
|
+
switch jstype {
|
|
31
|
+
case 0:
|
|
32
|
+
case 1:
|
|
33
|
+
return nil;
|
|
34
|
+
case 2:
|
|
35
|
+
return invoked.Bool();
|
|
36
|
+
case 3:
|
|
37
|
+
return invoked.Float();
|
|
38
|
+
case 4:
|
|
39
|
+
return invoked.String();
|
|
40
|
+
// currently unsupported
|
|
41
|
+
case 5:
|
|
42
|
+
case 6:
|
|
43
|
+
case 7:
|
|
44
|
+
return nil;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return nil;
|
|
48
|
+
},
|
|
49
|
+
// Json
|
|
50
|
+
func() map[string]any {
|
|
51
|
+
return properties;
|
|
52
|
+
},
|
|
53
|
+
// Set
|
|
54
|
+
func(key string, value any) error {
|
|
55
|
+
r := session.Get("set").Invoke(key, value);
|
|
56
|
+
if (r.Type() == 7) {
|
|
57
|
+
return errors.New(r.Invoke().String());
|
|
58
|
+
}
|
|
59
|
+
return nil
|
|
60
|
+
},
|
|
61
|
+
// Create
|
|
62
|
+
func(data map[string]any) {
|
|
63
|
+
session.Get("create").Invoke(data);
|
|
64
|
+
},
|
|
65
|
+
// Destroy
|
|
66
|
+
func() {
|
|
67
|
+
session.Get("destroy").Invoke();
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
package/src/runtime.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import exclude from "@rcompat/object/exclude";
|
|
2
|
+
import stringify from "@rcompat/object/stringify";
|
|
3
|
+
import valmap from "@rcompat/object/valmap";
|
|
4
|
+
|
|
5
|
+
const to_search_params = url =>
|
|
6
|
+
stringify(Object.fromEntries(url.searchParams.entries()));
|
|
7
|
+
const dispatchers = ["path", "query", "cookies", "headers"];
|
|
8
|
+
|
|
9
|
+
const to_dispatcher = dispatcher =>
|
|
10
|
+
valmap(exclude(dispatcher, ["get", "raw"]), getter => {
|
|
11
|
+
try {
|
|
12
|
+
return getter();
|
|
13
|
+
} catch ({ message }) {
|
|
14
|
+
return () => message;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const make_session = session => {
|
|
19
|
+
if (session === undefined) {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
session: {
|
|
25
|
+
...session,
|
|
26
|
+
set: (key, value) => {
|
|
27
|
+
try {
|
|
28
|
+
return session.set(key, value);
|
|
29
|
+
} catch ({ message }) {
|
|
30
|
+
return () => message;
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
get: session.get,
|
|
34
|
+
stringified: session.toString(),
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default request => {
|
|
40
|
+
dispatchers.map(property => to_dispatcher(request[property]));
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
url: request.url,
|
|
44
|
+
search_params: to_search_params(request.url),
|
|
45
|
+
body: JSON.stringify(request.body),
|
|
46
|
+
...Object.fromEntries(dispatchers.map(property => [
|
|
47
|
+
property, {
|
|
48
|
+
stringified: request[property].toString(),
|
|
49
|
+
...to_dispatcher(request[property]),
|
|
50
|
+
},
|
|
51
|
+
])),
|
|
52
|
+
...make_session(request.session),
|
|
53
|
+
};
|
|
54
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import error from "@primate/core/handler/error";
|
|
2
|
+
import redirect from "@primate/core/handler/redirect";
|
|
3
|
+
import view from "@primate/core/handler/view";
|
|
4
|
+
|
|
5
|
+
const handlers = {
|
|
6
|
+
view({ component, props = "{}", options = "{}" }) {
|
|
7
|
+
return view(component, JSON.parse(props), JSON.parse(options));
|
|
8
|
+
},
|
|
9
|
+
redirect({ location, options = "{}" }) {
|
|
10
|
+
return redirect(location, JSON.parse(options));
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const handle_function = response => {
|
|
15
|
+
const { handler, ...args } = response;
|
|
16
|
+
return handlers[handler]?.(args) ?? error();
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const handle_other = response => JSON.parse(response);
|
|
20
|
+
|
|
21
|
+
export default response => typeof response === "function"
|
|
22
|
+
? handle_function(response())
|
|
23
|
+
: handle_other(response);
|
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
// Copyright 2018 The Go Authors. All rights reserved.
|
|
2
|
+
// Use of this source code is governed by a BSD-style
|
|
3
|
+
// license that can be found in the LICENSE file.
|
|
4
|
+
|
|
5
|
+
export default () => {
|
|
6
|
+
const enosys = () => {
|
|
7
|
+
const err = new Error("not implemented");
|
|
8
|
+
err.code = "ENOSYS";
|
|
9
|
+
return err;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
if (!globalThis.fs) {
|
|
13
|
+
let outputBuf = "";
|
|
14
|
+
globalThis.fs = {
|
|
15
|
+
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
|
|
16
|
+
writeSync(fd, buf) {
|
|
17
|
+
outputBuf += decoder.decode(buf);
|
|
18
|
+
const nl = outputBuf.lastIndexOf("\n");
|
|
19
|
+
if (nl != -1) {
|
|
20
|
+
console.log(outputBuf.substring(0, nl));
|
|
21
|
+
outputBuf = outputBuf.substring(nl + 1);
|
|
22
|
+
}
|
|
23
|
+
return buf.length;
|
|
24
|
+
},
|
|
25
|
+
write(fd, buf, offset, length, position, callback) {
|
|
26
|
+
if (offset !== 0 || length !== buf.length || position !== null) {
|
|
27
|
+
callback(enosys());
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const n = this.writeSync(fd, buf);
|
|
31
|
+
callback(null, n);
|
|
32
|
+
},
|
|
33
|
+
chmod(path, mode, callback) { callback(enosys()); },
|
|
34
|
+
chown(path, uid, gid, callback) { callback(enosys()); },
|
|
35
|
+
close(fd, callback) { callback(enosys()); },
|
|
36
|
+
fchmod(fd, mode, callback) { callback(enosys()); },
|
|
37
|
+
fchown(fd, uid, gid, callback) { callback(enosys()); },
|
|
38
|
+
fstat(fd, callback) { callback(enosys()); },
|
|
39
|
+
fsync(fd, callback) { callback(null); },
|
|
40
|
+
ftruncate(fd, length, callback) { callback(enosys()); },
|
|
41
|
+
lchown(path, uid, gid, callback) { callback(enosys()); },
|
|
42
|
+
link(path, link, callback) { callback(enosys()); },
|
|
43
|
+
lstat(path, callback) { callback(enosys()); },
|
|
44
|
+
mkdir(path, perm, callback) { callback(enosys()); },
|
|
45
|
+
open(path, flags, mode, callback) { callback(enosys()); },
|
|
46
|
+
read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
|
|
47
|
+
readdir(path, callback) { callback(enosys()); },
|
|
48
|
+
readlink(path, callback) { callback(enosys()); },
|
|
49
|
+
rename(from, to, callback) { callback(enosys()); },
|
|
50
|
+
rmdir(path, callback) { callback(enosys()); },
|
|
51
|
+
stat(path, callback) { callback(enosys()); },
|
|
52
|
+
symlink(path, link, callback) { callback(enosys()); },
|
|
53
|
+
truncate(path, length, callback) { callback(enosys()); },
|
|
54
|
+
unlink(path, callback) { callback(enosys()); },
|
|
55
|
+
utimes(path, atime, mtime, callback) { callback(enosys()); },
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!globalThis.process) {
|
|
60
|
+
globalThis.process = {
|
|
61
|
+
getuid() { return -1; },
|
|
62
|
+
getgid() { return -1; },
|
|
63
|
+
geteuid() { return -1; },
|
|
64
|
+
getegid() { return -1; },
|
|
65
|
+
getgroups() { throw enosys(); },
|
|
66
|
+
pid: -1,
|
|
67
|
+
ppid: -1,
|
|
68
|
+
umask() { throw enosys(); },
|
|
69
|
+
cwd() { throw enosys(); },
|
|
70
|
+
chdir() { throw enosys(); },
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!globalThis.crypto) {
|
|
75
|
+
throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!globalThis.performance) {
|
|
79
|
+
throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!globalThis.TextEncoder) {
|
|
83
|
+
throw new Error("globalThis.TextEncoder is not available, polyfill required");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!globalThis.TextDecoder) {
|
|
87
|
+
throw new Error("globalThis.TextDecoder is not available, polyfill required");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const encoder = new TextEncoder("utf-8");
|
|
91
|
+
const decoder = new TextDecoder("utf-8");
|
|
92
|
+
|
|
93
|
+
globalThis.Go = class {
|
|
94
|
+
constructor() {
|
|
95
|
+
this.argv = ["js"];
|
|
96
|
+
this.env = {};
|
|
97
|
+
this.exit = (code) => {
|
|
98
|
+
if (code !== 0) {
|
|
99
|
+
console.warn("exit code:", code);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
this._exitPromise = new Promise((resolve) => {
|
|
103
|
+
this._resolveExitPromise = resolve;
|
|
104
|
+
});
|
|
105
|
+
this._pendingEvent = null;
|
|
106
|
+
this._scheduledTimeouts = new Map();
|
|
107
|
+
this._nextCallbackTimeoutID = 1;
|
|
108
|
+
|
|
109
|
+
const setInt64 = (addr, v) => {
|
|
110
|
+
this.mem.setUint32(addr + 0, v, true);
|
|
111
|
+
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const setInt32 = (addr, v) => {
|
|
115
|
+
this.mem.setUint32(addr + 0, v, true);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const getInt64 = (addr) => {
|
|
119
|
+
const low = this.mem.getUint32(addr + 0, true);
|
|
120
|
+
const high = this.mem.getInt32(addr + 4, true);
|
|
121
|
+
return low + high * 4294967296;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const loadValue = (addr) => {
|
|
125
|
+
const f = this.mem.getFloat64(addr, true);
|
|
126
|
+
if (f === 0) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
if (!isNaN(f)) {
|
|
130
|
+
return f;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const id = this.mem.getUint32(addr, true);
|
|
134
|
+
return this._values[id];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const storeValue = (addr, v) => {
|
|
138
|
+
const nanHead = 0x7FF80000;
|
|
139
|
+
|
|
140
|
+
if (typeof v === "number" && v !== 0) {
|
|
141
|
+
if (isNaN(v)) {
|
|
142
|
+
this.mem.setUint32(addr + 4, nanHead, true);
|
|
143
|
+
this.mem.setUint32(addr, 0, true);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
this.mem.setFloat64(addr, v, true);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (v === undefined) {
|
|
151
|
+
this.mem.setFloat64(addr, 0, true);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let id = this._ids.get(v);
|
|
156
|
+
if (id === undefined) {
|
|
157
|
+
id = this._idPool.pop();
|
|
158
|
+
if (id === undefined) {
|
|
159
|
+
id = this._values.length;
|
|
160
|
+
}
|
|
161
|
+
this._values[id] = v;
|
|
162
|
+
this._goRefCounts[id] = 0;
|
|
163
|
+
this._ids.set(v, id);
|
|
164
|
+
}
|
|
165
|
+
this._goRefCounts[id]++;
|
|
166
|
+
let typeFlag = 0;
|
|
167
|
+
switch (typeof v) {
|
|
168
|
+
case "object":
|
|
169
|
+
if (v !== null) {
|
|
170
|
+
typeFlag = 1;
|
|
171
|
+
}
|
|
172
|
+
break;
|
|
173
|
+
case "string":
|
|
174
|
+
typeFlag = 2;
|
|
175
|
+
break;
|
|
176
|
+
case "symbol":
|
|
177
|
+
typeFlag = 3;
|
|
178
|
+
break;
|
|
179
|
+
case "function":
|
|
180
|
+
typeFlag = 4;
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
|
|
184
|
+
this.mem.setUint32(addr, id, true);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const loadSlice = (addr) => {
|
|
188
|
+
const array = getInt64(addr + 0);
|
|
189
|
+
const len = getInt64(addr + 8);
|
|
190
|
+
return new Uint8Array(this._inst.exports.mem.buffer, array, len);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const loadSliceOfValues = (addr) => {
|
|
194
|
+
const array = getInt64(addr + 0);
|
|
195
|
+
const len = getInt64(addr + 8);
|
|
196
|
+
const a = new Array(len);
|
|
197
|
+
for (let i = 0; i < len; i++) {
|
|
198
|
+
a[i] = loadValue(array + i * 8);
|
|
199
|
+
}
|
|
200
|
+
return a;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const loadString = (addr) => {
|
|
204
|
+
const saddr = getInt64(addr + 0);
|
|
205
|
+
const len = getInt64(addr + 8);
|
|
206
|
+
return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const timeOrigin = Date.now() - performance.now();
|
|
210
|
+
this.importObject = {
|
|
211
|
+
_gotest: {
|
|
212
|
+
add: (a, b) => a + b,
|
|
213
|
+
},
|
|
214
|
+
gojs: {
|
|
215
|
+
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
|
|
216
|
+
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
|
|
217
|
+
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
|
|
218
|
+
// This changes the SP, thus we have to update the SP used by the imported function.
|
|
219
|
+
|
|
220
|
+
// func wasmExit(code int32)
|
|
221
|
+
"runtime.wasmExit": (sp) => {
|
|
222
|
+
sp >>>= 0;
|
|
223
|
+
const code = this.mem.getInt32(sp + 8, true);
|
|
224
|
+
this.exited = true;
|
|
225
|
+
delete this._inst;
|
|
226
|
+
delete this._values;
|
|
227
|
+
delete this._goRefCounts;
|
|
228
|
+
delete this._ids;
|
|
229
|
+
delete this._idPool;
|
|
230
|
+
this.exit(code);
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
|
|
234
|
+
"runtime.wasmWrite": (sp) => {
|
|
235
|
+
sp >>>= 0;
|
|
236
|
+
const fd = getInt64(sp + 8);
|
|
237
|
+
const p = getInt64(sp + 16);
|
|
238
|
+
const n = this.mem.getInt32(sp + 24, true);
|
|
239
|
+
fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
|
|
240
|
+
},
|
|
241
|
+
|
|
242
|
+
// func resetMemoryDataView()
|
|
243
|
+
"runtime.resetMemoryDataView": (sp) => {
|
|
244
|
+
sp >>>= 0;
|
|
245
|
+
this.mem = new DataView(this._inst.exports.mem.buffer);
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
// func nanotime1() int64
|
|
249
|
+
"runtime.nanotime1": (sp) => {
|
|
250
|
+
sp >>>= 0;
|
|
251
|
+
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
|
|
252
|
+
},
|
|
253
|
+
|
|
254
|
+
// func walltime() (sec int64, nsec int32)
|
|
255
|
+
"runtime.walltime": (sp) => {
|
|
256
|
+
sp >>>= 0;
|
|
257
|
+
const msec = (new Date).getTime();
|
|
258
|
+
setInt64(sp + 8, msec / 1000);
|
|
259
|
+
this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
|
|
260
|
+
},
|
|
261
|
+
|
|
262
|
+
// func scheduleTimeoutEvent(delay int64) int32
|
|
263
|
+
"runtime.scheduleTimeoutEvent": (sp) => {
|
|
264
|
+
sp >>>= 0;
|
|
265
|
+
const id = this._nextCallbackTimeoutID;
|
|
266
|
+
this._nextCallbackTimeoutID++;
|
|
267
|
+
this._scheduledTimeouts.set(id, setTimeout(
|
|
268
|
+
() => {
|
|
269
|
+
this._resume();
|
|
270
|
+
while (this._scheduledTimeouts.has(id)) {
|
|
271
|
+
// for some reason Go failed to register the timeout event, log and try again
|
|
272
|
+
// (temporary workaround for https://github.com/golang/go/issues/28975)
|
|
273
|
+
console.warn("scheduleTimeoutEvent: missed timeout event");
|
|
274
|
+
this._resume();
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
getInt64(sp + 8),
|
|
278
|
+
));
|
|
279
|
+
this.mem.setInt32(sp + 16, id, true);
|
|
280
|
+
},
|
|
281
|
+
|
|
282
|
+
// func clearTimeoutEvent(id int32)
|
|
283
|
+
"runtime.clearTimeoutEvent": (sp) => {
|
|
284
|
+
sp >>>= 0;
|
|
285
|
+
const id = this.mem.getInt32(sp + 8, true);
|
|
286
|
+
clearTimeout(this._scheduledTimeouts.get(id));
|
|
287
|
+
this._scheduledTimeouts.delete(id);
|
|
288
|
+
},
|
|
289
|
+
|
|
290
|
+
// func getRandomData(r []byte)
|
|
291
|
+
"runtime.getRandomData": (sp) => {
|
|
292
|
+
sp >>>= 0;
|
|
293
|
+
crypto.getRandomValues(loadSlice(sp + 8));
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
// func finalizeRef(v ref)
|
|
297
|
+
"syscall/js.finalizeRef": (sp) => {
|
|
298
|
+
sp >>>= 0;
|
|
299
|
+
const id = this.mem.getUint32(sp + 8, true);
|
|
300
|
+
this._goRefCounts[id]--;
|
|
301
|
+
if (this._goRefCounts[id] === 0) {
|
|
302
|
+
const v = this._values[id];
|
|
303
|
+
this._values[id] = null;
|
|
304
|
+
this._ids.delete(v);
|
|
305
|
+
this._idPool.push(id);
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
|
|
309
|
+
// func stringVal(value string) ref
|
|
310
|
+
"syscall/js.stringVal": (sp) => {
|
|
311
|
+
sp >>>= 0;
|
|
312
|
+
storeValue(sp + 24, loadString(sp + 8));
|
|
313
|
+
},
|
|
314
|
+
|
|
315
|
+
// func valueGet(v ref, p string) ref
|
|
316
|
+
"syscall/js.valueGet": (sp) => {
|
|
317
|
+
sp >>>= 0;
|
|
318
|
+
const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
|
|
319
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
|
320
|
+
storeValue(sp + 32, result);
|
|
321
|
+
},
|
|
322
|
+
|
|
323
|
+
// func valueSet(v ref, p string, x ref)
|
|
324
|
+
"syscall/js.valueSet": (sp) => {
|
|
325
|
+
sp >>>= 0;
|
|
326
|
+
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
|
|
327
|
+
},
|
|
328
|
+
|
|
329
|
+
// func valueDelete(v ref, p string)
|
|
330
|
+
"syscall/js.valueDelete": (sp) => {
|
|
331
|
+
sp >>>= 0;
|
|
332
|
+
Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
|
|
333
|
+
},
|
|
334
|
+
|
|
335
|
+
// func valueIndex(v ref, i int) ref
|
|
336
|
+
"syscall/js.valueIndex": (sp) => {
|
|
337
|
+
sp >>>= 0;
|
|
338
|
+
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
|
|
339
|
+
},
|
|
340
|
+
|
|
341
|
+
// valueSetIndex(v ref, i int, x ref)
|
|
342
|
+
"syscall/js.valueSetIndex": (sp) => {
|
|
343
|
+
sp >>>= 0;
|
|
344
|
+
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
|
|
345
|
+
},
|
|
346
|
+
|
|
347
|
+
// func valueCall(v ref, m string, args []ref) (ref, bool)
|
|
348
|
+
"syscall/js.valueCall": (sp) => {
|
|
349
|
+
sp >>>= 0;
|
|
350
|
+
try {
|
|
351
|
+
const v = loadValue(sp + 8);
|
|
352
|
+
const m = Reflect.get(v, loadString(sp + 16));
|
|
353
|
+
const args = loadSliceOfValues(sp + 32);
|
|
354
|
+
const result = Reflect.apply(m, v, args);
|
|
355
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
|
356
|
+
storeValue(sp + 56, result);
|
|
357
|
+
this.mem.setUint8(sp + 64, 1);
|
|
358
|
+
} catch (err) {
|
|
359
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
|
360
|
+
storeValue(sp + 56, err);
|
|
361
|
+
this.mem.setUint8(sp + 64, 0);
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
|
|
365
|
+
// func valueInvoke(v ref, args []ref) (ref, bool)
|
|
366
|
+
"syscall/js.valueInvoke": (sp) => {
|
|
367
|
+
sp >>>= 0;
|
|
368
|
+
try {
|
|
369
|
+
const v = loadValue(sp + 8);
|
|
370
|
+
const args = loadSliceOfValues(sp + 16);
|
|
371
|
+
const result = Reflect.apply(v, undefined, args);
|
|
372
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
|
373
|
+
storeValue(sp + 40, result);
|
|
374
|
+
this.mem.setUint8(sp + 48, 1);
|
|
375
|
+
} catch (err) {
|
|
376
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
|
377
|
+
storeValue(sp + 40, err);
|
|
378
|
+
this.mem.setUint8(sp + 48, 0);
|
|
379
|
+
}
|
|
380
|
+
},
|
|
381
|
+
|
|
382
|
+
// func valueNew(v ref, args []ref) (ref, bool)
|
|
383
|
+
"syscall/js.valueNew": (sp) => {
|
|
384
|
+
sp >>>= 0;
|
|
385
|
+
try {
|
|
386
|
+
const v = loadValue(sp + 8);
|
|
387
|
+
const args = loadSliceOfValues(sp + 16);
|
|
388
|
+
const result = Reflect.construct(v, args);
|
|
389
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
|
390
|
+
storeValue(sp + 40, result);
|
|
391
|
+
this.mem.setUint8(sp + 48, 1);
|
|
392
|
+
} catch (err) {
|
|
393
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
|
394
|
+
storeValue(sp + 40, err);
|
|
395
|
+
this.mem.setUint8(sp + 48, 0);
|
|
396
|
+
}
|
|
397
|
+
},
|
|
398
|
+
|
|
399
|
+
// func valueLength(v ref) int
|
|
400
|
+
"syscall/js.valueLength": (sp) => {
|
|
401
|
+
sp >>>= 0;
|
|
402
|
+
setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
|
|
403
|
+
},
|
|
404
|
+
|
|
405
|
+
// valuePrepareString(v ref) (ref, int)
|
|
406
|
+
"syscall/js.valuePrepareString": (sp) => {
|
|
407
|
+
sp >>>= 0;
|
|
408
|
+
const str = encoder.encode(String(loadValue(sp + 8)));
|
|
409
|
+
storeValue(sp + 16, str);
|
|
410
|
+
setInt64(sp + 24, str.length);
|
|
411
|
+
},
|
|
412
|
+
|
|
413
|
+
// valueLoadString(v ref, b []byte)
|
|
414
|
+
"syscall/js.valueLoadString": (sp) => {
|
|
415
|
+
sp >>>= 0;
|
|
416
|
+
const str = loadValue(sp + 8);
|
|
417
|
+
loadSlice(sp + 16).set(str);
|
|
418
|
+
},
|
|
419
|
+
|
|
420
|
+
// func valueInstanceOf(v ref, t ref) bool
|
|
421
|
+
"syscall/js.valueInstanceOf": (sp) => {
|
|
422
|
+
sp >>>= 0;
|
|
423
|
+
this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
|
|
424
|
+
},
|
|
425
|
+
|
|
426
|
+
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
|
427
|
+
"syscall/js.copyBytesToGo": (sp) => {
|
|
428
|
+
sp >>>= 0;
|
|
429
|
+
const dst = loadSlice(sp + 8);
|
|
430
|
+
const src = loadValue(sp + 32);
|
|
431
|
+
if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
|
|
432
|
+
this.mem.setUint8(sp + 48, 0);
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
const toCopy = src.subarray(0, dst.length);
|
|
436
|
+
dst.set(toCopy);
|
|
437
|
+
setInt64(sp + 40, toCopy.length);
|
|
438
|
+
this.mem.setUint8(sp + 48, 1);
|
|
439
|
+
},
|
|
440
|
+
|
|
441
|
+
// func copyBytesToJS(dst ref, src []byte) (int, bool)
|
|
442
|
+
"syscall/js.copyBytesToJS": (sp) => {
|
|
443
|
+
sp >>>= 0;
|
|
444
|
+
const dst = loadValue(sp + 8);
|
|
445
|
+
const src = loadSlice(sp + 16);
|
|
446
|
+
if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
|
|
447
|
+
this.mem.setUint8(sp + 48, 0);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
const toCopy = src.subarray(0, dst.length);
|
|
451
|
+
dst.set(toCopy);
|
|
452
|
+
setInt64(sp + 40, toCopy.length);
|
|
453
|
+
this.mem.setUint8(sp + 48, 1);
|
|
454
|
+
},
|
|
455
|
+
|
|
456
|
+
"debug": (value) => {
|
|
457
|
+
console.log(value);
|
|
458
|
+
},
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
async run(instance) {
|
|
464
|
+
if (!(instance instanceof WebAssembly.Instance)) {
|
|
465
|
+
throw new Error("Go.run: WebAssembly.Instance expected");
|
|
466
|
+
}
|
|
467
|
+
this._inst = instance;
|
|
468
|
+
this.mem = new DataView(this._inst.exports.mem.buffer);
|
|
469
|
+
this._values = [ // JS values that Go currently has references to, indexed by reference id
|
|
470
|
+
NaN,
|
|
471
|
+
0,
|
|
472
|
+
null,
|
|
473
|
+
true,
|
|
474
|
+
false,
|
|
475
|
+
globalThis,
|
|
476
|
+
this,
|
|
477
|
+
];
|
|
478
|
+
this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
|
|
479
|
+
this._ids = new Map([ // mapping from JS values to reference ids
|
|
480
|
+
[0, 1],
|
|
481
|
+
[null, 2],
|
|
482
|
+
[true, 3],
|
|
483
|
+
[false, 4],
|
|
484
|
+
[globalThis, 5],
|
|
485
|
+
[this, 6],
|
|
486
|
+
]);
|
|
487
|
+
this._idPool = []; // unused ids that have been garbage collected
|
|
488
|
+
this.exited = false; // whether the Go program has exited
|
|
489
|
+
|
|
490
|
+
// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
|
|
491
|
+
let offset = 4096;
|
|
492
|
+
|
|
493
|
+
const strPtr = (str) => {
|
|
494
|
+
const ptr = offset;
|
|
495
|
+
const bytes = encoder.encode(str + "\0");
|
|
496
|
+
new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
|
|
497
|
+
offset += bytes.length;
|
|
498
|
+
if (offset % 8 !== 0) {
|
|
499
|
+
offset += 8 - (offset % 8);
|
|
500
|
+
}
|
|
501
|
+
return ptr;
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
const argc = this.argv.length;
|
|
505
|
+
|
|
506
|
+
const argvPtrs = [];
|
|
507
|
+
this.argv.forEach((arg) => {
|
|
508
|
+
argvPtrs.push(strPtr(arg));
|
|
509
|
+
});
|
|
510
|
+
argvPtrs.push(0);
|
|
511
|
+
|
|
512
|
+
const keys = Object.keys(this.env).sort();
|
|
513
|
+
keys.forEach((key) => {
|
|
514
|
+
argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
|
|
515
|
+
});
|
|
516
|
+
argvPtrs.push(0);
|
|
517
|
+
|
|
518
|
+
const argv = offset;
|
|
519
|
+
argvPtrs.forEach((ptr) => {
|
|
520
|
+
this.mem.setUint32(offset, ptr, true);
|
|
521
|
+
this.mem.setUint32(offset + 4, 0, true);
|
|
522
|
+
offset += 8;
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
// The linker guarantees global data starts from at least wasmMinDataAddr.
|
|
526
|
+
// Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
|
|
527
|
+
const wasmMinDataAddr = 4096 + 8192;
|
|
528
|
+
if (offset >= wasmMinDataAddr) {
|
|
529
|
+
throw new Error("total length of command line and environment variables exceeds limit");
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
this._inst.exports.run(argc, argv);
|
|
533
|
+
if (this.exited) {
|
|
534
|
+
this._resolveExitPromise();
|
|
535
|
+
}
|
|
536
|
+
await this._exitPromise;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
_resume() {
|
|
540
|
+
if (this.exited) {
|
|
541
|
+
throw new Error("Go program has already exited");
|
|
542
|
+
}
|
|
543
|
+
this._inst.exports.resume();
|
|
544
|
+
if (this.exited) {
|
|
545
|
+
this._resolveExitPromise();
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
_makeFuncWrapper(id) {
|
|
550
|
+
const go = this;
|
|
551
|
+
return function () {
|
|
552
|
+
const event = { id: id, this: this, args: arguments };
|
|
553
|
+
go._pendingEvent = event;
|
|
554
|
+
go._resume();
|
|
555
|
+
return event.result;
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
};
|