This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
/
generate.go
1053 lines (917 loc) · 24.9 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
package gowasm2cpp
import (
"bytes"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/go-interpreter/wagon/wasm"
"golang.org/x/sync/errgroup"
)
func identifierFromString(str string) string {
var ident string
for _, r := range []rune(str) {
if r > 0xff {
panic("identifiers cannot include non-Latin1 characters")
}
if '0' <= r && r <= '9' {
ident += string(r)
continue
}
if 'a' <= r && r <= 'z' {
ident += string(r)
continue
}
if 'A' <= r && r <= 'Z' {
ident += string(r)
continue
}
ident += fmt.Sprintf("_%02x", r)
}
if len(ident) > 512 {
ident = ident[:511]
}
return ident
}
func includeGuard(str string) string {
return strings.ToUpper(str)
}
type wasmFunc struct {
Mod *wasm.Module
Funcs []*wasmFunc
Types []*wasmType
Globals []*wasmGlobal
Type *wasmType
Wasm wasm.Function
Index int
Import bool
BodyStr string
}
func (f *wasmFunc) Identifier() string {
return identifierFromString(f.Wasm.Name)
}
var funcDeclTmpl = template.Must(template.New("funcDecl").Parse(`// OriginalName: {{.OriginalName}}
// Index: {{.Index}}
{{if .Abstract}}virtual {{end}}{{.ReturnType}} {{.Name}}({{.Args}}){{if .Abstract}} = 0{{end}}{{if .Override}} override{{end}};`))
var funcImplTmpl = template.Must(template.New("func").Parse(`// OriginalName: {{.OriginalName}}
// Index: {{.Index}}
{{.ReturnType}} {{.Class}}::{{.Name}}({{.Args}}) {
{{range .Locals}} {{.}}
{{end}}{{if .Locals}}
{{end}}{{range .Body}}{{.}}
{{end}}}`))
func wasmTypeToReturnType(v wasm.ValueType) returnType {
switch v {
case wasm.ValueTypeI32:
return returnTypeI32
case wasm.ValueTypeI64:
return returnTypeI64
case wasm.ValueTypeF32:
return returnTypeF32
case wasm.ValueTypeF64:
return returnTypeF64
default:
panic("not reached")
}
}
func (f *wasmFunc) CppDecl(indent string, abstract bool, override bool) (string, error) {
var retType returnType
switch ts := f.Wasm.Sig.ReturnTypes; len(ts) {
case 0:
retType = returnTypeVoid
case 1:
retType = wasmTypeToReturnType(ts[0])
default:
return "", fmt.Errorf("the number of return values must be 0 or 1 but %d", len(ts))
}
var args []string
for i, t := range f.Wasm.Sig.ParamTypes {
args = append(args, fmt.Sprintf("%s local%d_", wasmTypeToReturnType(t).Cpp(), i))
}
var buf bytes.Buffer
if err := funcDeclTmpl.Execute(&buf, struct {
OriginalName string
Name string
Index int
ReturnType string
Args string
Abstract bool
Override bool
}{
OriginalName: f.Wasm.Name,
Name: identifierFromString(f.Wasm.Name),
Index: f.Index,
ReturnType: retType.Cpp(),
Args: strings.Join(args, ", "),
Abstract: abstract,
Override: override,
}); err != nil {
return "", err
}
// Add indentations
var lines []string
for _, line := range strings.Split(buf.String(), "\n") {
lines = append(lines, indent+line)
}
return strings.Join(lines, "\n"), nil
}
func (f *wasmFunc) CppImpl(className string, indent string) (string, error) {
var retType returnType
switch ts := f.Wasm.Sig.ReturnTypes; len(ts) {
case 0:
retType = returnTypeVoid
case 1:
retType = wasmTypeToReturnType(ts[0])
default:
return "", fmt.Errorf("the number of return values must be 0 or 1 but %d", len(ts))
}
var args []string
for i, t := range f.Wasm.Sig.ParamTypes {
args = append(args, fmt.Sprintf("%s local%d_", wasmTypeToReturnType(t).Cpp(), i))
}
var locals []string
var body []string
if f.BodyStr != "" {
body = strings.Split(f.BodyStr, "\n")
} else if f.Wasm.Body != nil {
idx := len(f.Wasm.Sig.ParamTypes)
for _, e := range f.Wasm.Body.Locals {
for i := 0; i < int(e.Count); i++ {
locals = append(locals, fmt.Sprintf("%s local%d_ = 0;", wasmTypeToReturnType(e.Type).Cpp(), idx))
idx++
}
}
var err error
body, err = f.bodyToCpp()
if err != nil {
return "", err
}
locals = removeUnusedLocalVariables(locals, body)
} else {
// TODO: Use error function.
ident := identifierFromString(f.Wasm.Name)
body = []string{
fmt.Sprintf(` std::cerr << "%s not implemented" << std::endl;`, ident),
" std::exit(1);"}
}
var buf bytes.Buffer
if err := funcImplTmpl.Execute(&buf, struct {
OriginalName string
Name string
Class string
Index int
ReturnType string
Args string
Locals []string
Body []string
}{
OriginalName: f.Wasm.Name,
Name: identifierFromString(f.Wasm.Name),
Class: className,
Index: f.Index,
ReturnType: retType.Cpp(),
Args: strings.Join(args, ", "),
Locals: locals,
Body: body,
}); err != nil {
return "", err
}
// Add indentations
var lines []string
for _, line := range strings.Split(buf.String(), "\n") {
lines = append(lines, indent+line)
}
return strings.Join(lines, "\n") + "\n", nil
}
var (
localVariableRe = regexp.MustCompile(`local[0-9]+_`)
)
func removeUnusedLocalVariables(decls []string, body []string) []string {
decl2name := map[string]string{}
for _, d := range decls {
decl2name[d] = localVariableRe.FindString(d)
}
unused := map[string]struct{}{}
for _, n := range decl2name {
unused[n] = struct{}{}
}
for _, l := range body {
for _, v := range localVariableRe.FindAllString(l, -1) {
delete(unused, v)
}
}
if len(unused) == 0 {
return decls
}
r := make([]string, 0, len(decls)-len(unused))
for _, d := range decls {
v := decl2name[d]
if _, ok := unused[v]; ok {
continue
}
r = append(r, d)
}
return r
}
type wasmExport struct {
Funcs []*wasmFunc
Index int
Name string
}
func (e *wasmExport) CppDecl(indent string) (string, error) {
f := e.Funcs[e.Index]
var retType returnType
switch ts := f.Wasm.Sig.ReturnTypes; len(ts) {
case 0:
retType = returnTypeVoid
case 1:
retType = wasmTypeToReturnType(ts[0])
default:
return "", fmt.Errorf("the number of return values must be 0 or 1 but %d", len(ts))
}
var args []string
for i, t := range f.Wasm.Sig.ParamTypes {
args = append(args, fmt.Sprintf("%s arg%d", wasmTypeToReturnType(t).Cpp(), i))
}
str := fmt.Sprintf(`%s %s(%s);`, retType.Cpp(), e.Name, strings.Join(args, ", "))
lines := strings.Split(str, "\n")
for i := range lines {
lines[i] = indent + lines[i]
}
return strings.Join(lines, "\n"), nil
}
func (e *wasmExport) CppImpl(indent string) (string, error) {
f := e.Funcs[e.Index]
var ret string
var retType returnType
switch ts := f.Wasm.Sig.ReturnTypes; len(ts) {
case 0:
retType = returnTypeVoid
case 1:
ret = "return "
retType = wasmTypeToReturnType(ts[0])
default:
return "", fmt.Errorf("the number of return values must be 0 or 1 but %d", len(ts))
}
var args []string
var argsToPass []string
for i, t := range f.Wasm.Sig.ParamTypes {
args = append(args, fmt.Sprintf("%s arg%d", wasmTypeToReturnType(t).Cpp(), i))
argsToPass = append(argsToPass, fmt.Sprintf("arg%d", i))
}
str := fmt.Sprintf(`%s Inst::%s(%s) {
%s%s(%s);
}
`, retType.Cpp(), e.Name, strings.Join(args, ", "), ret, identifierFromString(f.Wasm.Name), strings.Join(argsToPass, ", "))
lines := strings.Split(str, "\n")
for i := range lines {
lines[i] = indent + lines[i]
}
return strings.Join(lines, "\n"), nil
}
type wasmGlobal struct {
Type wasm.ValueType
Index int
Init int
}
func (g *wasmGlobal) Cpp() string {
return fmt.Sprintf("%s global%d_ = %d;", wasmTypeToReturnType(g.Type).Cpp(), g.Index, g.Init)
}
type wasmType struct {
Sig *wasm.FunctionSig
Index int
}
func (t *wasmType) Cpp() (string, error) {
var retType returnType
switch ts := t.Sig.ReturnTypes; len(ts) {
case 0:
retType = returnTypeVoid
case 1:
retType = wasmTypeToReturnType(ts[0])
default:
return "", fmt.Errorf("the number of return values must be 0 or 1 but %d", len(ts))
}
var args []string
for i, t := range t.Sig.ParamTypes {
args = append(args, fmt.Sprintf("%s arg%d", wasmTypeToReturnType(t).Cpp(), i))
}
return fmt.Sprintf("%s (Inst::*)(%s)", retType.Cpp(), strings.Join(args, ", ")), nil
}
func Generate(outDir string, include string, wasmFile string, namespace string) error {
f, err := os.Open(wasmFile)
if err != nil {
return err
}
defer f.Close()
mod, err := wasm.DecodeModule(f)
if err != nil {
return err
}
var types []*wasmType
for i, e := range mod.Types.Entries {
e := e
types = append(types, &wasmType{
Sig: &e,
Index: i,
})
}
var globals []*wasmGlobal
for i, e := range mod.Global.Globals {
// TODO: Consider mutability.
// TODO: Use e.Type.Init.
globals = append(globals, &wasmGlobal{
Type: e.Type.Type,
Index: i,
Init: 0,
})
}
var ifs []*wasmFunc
for i, e := range mod.Import.Entries {
name := e.FieldName
ifs = append(ifs, &wasmFunc{
Type: types[e.Type.(wasm.FuncImport).Type],
Wasm: wasm.Function{
Sig: types[e.Type.(wasm.FuncImport).Type].Sig,
Name: name,
},
Globals: globals,
Index: i,
Import: true,
BodyStr: importFuncBodies[name],
})
}
// There is a bug that signature and body are shifted (go-interpreter/wagon#190).
var names wasm.NameMap
if c := mod.Custom(wasm.CustomSectionName); c != nil {
var nsec wasm.NameSection
if err := nsec.UnmarshalWASM(bytes.NewReader(c.Data)); err != nil {
return err
}
if len(nsec.Types[wasm.NameFunction]) > 0 {
sub, err := nsec.Decode(wasm.NameFunction)
if err != nil {
return err
}
names = sub.(*wasm.FunctionNames).Names
}
}
var fs []*wasmFunc
for i, t := range mod.Function.Types {
name := names[uint32(i+len(mod.Import.Entries))]
bodyStr, ok := specialFunctionBodies[name]
var body *wasm.FunctionBody
if !ok {
body = &mod.Code.Bodies[i]
}
fs = append(fs, &wasmFunc{
Type: types[t],
Wasm: wasm.Function{
Sig: types[t].Sig,
Body: body,
Name: name,
},
Globals: globals,
Index: i + len(mod.Import.Entries),
BodyStr: bodyStr,
})
}
var exports []*wasmExport
for _, e := range mod.Export.Entries {
switch e.Kind {
case wasm.ExternalFunction:
exports = append(exports, &wasmExport{
Index: int(e.Index),
Name: e.FieldStr,
})
case wasm.ExternalMemory:
// Ignore
default:
return fmt.Errorf("export type %d is not implemented", e.Kind)
}
}
allfs := append(ifs, fs...)
for _, e := range exports {
e.Funcs = allfs
}
for _, f := range ifs {
f.Mod = mod
f.Funcs = allfs
f.Types = types
}
for _, f := range fs {
f.Mod = mod
f.Funcs = allfs
f.Types = types
}
if mod.Start != nil {
return fmt.Errorf("start section must be nil but not")
}
tables := make([][]uint32, len(mod.Table.Entries))
for _, e := range mod.Elements.Entries {
v, err := mod.ExecInitExpr(e.Offset)
if err != nil {
return err
}
offset := v.(int32)
if diff := int(offset) + int(len(e.Elems)) - int(len(tables[e.Index])); diff > 0 {
tables[e.Index] = append(tables[e.Index], make([]uint32, diff)...)
}
copy(tables[e.Index][offset:], e.Elems)
}
var data []wasmData
for _, e := range mod.Data.Entries {
offset, err := mod.ExecInitExpr(e.Offset)
if err != nil {
return err
}
data = append(data, wasmData{
Offset: int(offset.(int32)),
Data: e.Data,
})
}
var incpath string
if include != "" {
include = filepath.ToSlash(include)
if include[len(include)-1] != '/' {
include += "/"
}
}
var g errgroup.Group
g.Go(func() error {
{
out, err := os.Create(filepath.Join(outDir, "go.h"))
if err != nil {
return err
}
defer out.Close()
if err := goHTmpl.Execute(out, struct {
IncludeGuard string
IncludePath string
Namespace string
ImportFuncs []*wasmFunc
}{
IncludeGuard: includeGuard(namespace) + "_GO_H",
IncludePath: incpath,
Namespace: namespace,
ImportFuncs: ifs,
}); err != nil {
return err
}
}
{
out, err := os.Create(filepath.Join(outDir, "go.cpp"))
if err != nil {
return err
}
defer out.Close()
if err := goCppTmpl.Execute(out, struct {
IncludePath string
Namespace string
ImportFuncs []*wasmFunc
}{
IncludePath: incpath,
Namespace: namespace,
ImportFuncs: ifs,
}); err != nil {
return err
}
}
return nil
})
g.Go(func() error {
return writeBits(outDir, incpath, namespace)
})
g.Go(func() error {
return writeGame(outDir, incpath, namespace)
})
g.Go(func() error {
return writeGL(outDir, incpath, namespace)
})
g.Go(func() error {
return writeJS(outDir, incpath, namespace)
})
g.Go(func() error {
return writeTaskQueue(outDir, incpath, namespace)
})
g.Go(func() error {
return writeBytes(outDir, incpath, namespace)
})
g.Go(func() error {
return writeInst(outDir, incpath, namespace, ifs, fs, exports, globals, types, tables)
})
g.Go(func() error {
return writeMem(outDir, incpath, namespace, int(mod.Memory.Entries[0].Limits.Initial), data)
})
if err := g.Wait(); err != nil {
return err
}
return nil
}
var goHTmpl = template.Must(template.New("go.h").Parse(`// Code generated by go2cpp. DO NOT EDIT.
#ifndef {{.IncludeGuard}}
#define {{.IncludeGuard}}
#include "{{.IncludePath}}bytes.h"
#include "{{.IncludePath}}js.h"
#include "{{.IncludePath}}inst.h"
#include "{{.IncludePath}}mem.h"
#include "{{.IncludePath}}taskqueue.h"
#include <algorithm>
#include <cstdint>
#include <chrono>
#include <functional>
#include <map>
#include <memory>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace {{.Namespace}} {
class Mem;
class Go {
public:
Go();
Go(std::unique_ptr<Writer> debug_writer);
int Run();
int Run(int argc, char** argv);
int Run(const std::vector<std::string>& args);
// EnqueuTask is concurrent-safe.
void EnqueueTask(std::function<void()> task);
private:
class ImportImpl : public Import {
public:
explicit ImportImpl(Go* go);
{{range $value := .ImportFuncs}}{{$value.CppDecl " " false true}}
{{end}}
private:
Go* go_;
};
class GoObject : public Object {
public:
explicit GoObject(Go* go);
Value Get(const std::string& key) override;
void Set(const std::string& key, Value value) override;
std::string ToString() const override { return "GoObject"; }
private:
Go* go_;
Value func_make_func_wrapper_;
};
Value LoadValue(int32_t addr);
void StoreValue(int32_t addr, Value v);
std::vector<Value> LoadSliceOfValues(int32_t addr);
void Exit(int32_t code);
void Resume();
Value MakeFuncWrapper(int32_t id);
void DebugWrite(BytesSpan bytes);
int64_t PreciseNowInNanoseconds();
double UnixNowInMilliseconds();
int32_t SetTimeout(double interval);
void ClearTimeout(int32_t id);
void GetRandomBytes(BytesSpan bytes);
int32_t GetIdFromValue(const Value& value);
void GC();
ImportImpl import_;
std::unique_ptr<Writer> debug_writer_;
// A TaskQueue must be destructed after the timers are destructed.
TaskQueue task_queue_;
Value pending_event_{Value::Null()};
std::unordered_map<int32_t, std::unique_ptr<Timer>> scheduled_timeouts_;
int32_t next_callback_timeout_id_ = 1;
std::unique_ptr<Inst> inst_;
std::unique_ptr<Mem> mem_;
std::unordered_map<int32_t, Value> values_;
std::unordered_map<int32_t, double> go_ref_counts_;
std::unordered_map<Value, int32_t, Value::Hash> ids_;
std::unordered_set<int32_t> id_pool_;
int32_t next_id_;
// A set of Value IDs to be finalized later.
// Note that the ID is added to this set when Go no longer has this value,
// but the Value might still be alive on C++ side, and might be reused on Go side later.
// Value is a ref-counted object and even if a Value is removed from values_, the value might be alive.
std::unordered_set<int32_t> finalizing_ids_;
bool exited_ = false;
int32_t exit_code_ = 0;
std::chrono::high_resolution_clock::time_point start_time_point_ = std::chrono::high_resolution_clock::now();
};
}
#endif // {{.IncludeGuard}}
`))
var goCppTmpl = template.Must(template.New("go.cpp").Parse(`// Code generated by go2cpp. DO NOT EDIT.
#include "{{.IncludePath}}go.h"
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits>
#include <random>
namespace {{.Namespace}} {
namespace {
void error(const std::string& msg) {
std::cerr << msg << std::endl;
assert(false);
std::exit(1);
}
}
Go::Go()
: Go(std::make_unique<StreamWriter>(std::cerr)) {
}
Go::Go(std::unique_ptr<Writer> debug_writer)
: import_{this},
debug_writer_{std::move(debug_writer)} {
}
int Go::Run() {
return Run(std::vector<std::string>{});
}
int Go::Run(int argc, char** argv) {
std::vector<std::string> args(argv, argv + argc);
return Run(args);
}
int Go::Run(const std::vector<std::string>& args) {
mem_ = std::make_unique<Mem>();
inst_ = std::make_unique<Inst>(mem_.get(), &import_);
values_ = {
{0, Value{std::nan("")}},
{1, Value{0.0}},
{2, Value::Null()},
{3, Value{true}},
{4, Value{false}},
{5, Value::Global()},
{6, Value{std::make_unique<GoObject>(this)}},
};
next_id_ = values_.size();
static constexpr double inf = std::numeric_limits<double>::infinity();
go_ref_counts_ = {
{0, inf},
{1, inf},
{2, inf},
{3, inf},
{4, inf},
{5, inf},
{6, inf},
};
ids_ = {
{values_[1], 1},
{values_[2], 2},
{values_[3], 3},
{values_[4], 4},
{values_[5], 5},
{values_[6], 6},
};
id_pool_ = {};
exited_ = false;
exit_code_ = 0;
int32_t offset = 4096;
auto str_ptr = [this, &offset](const std::string& str) -> int32_t {
int32_t ptr = offset;
std::vector<uint8_t> bytes(str.begin(), str.end());
bytes.push_back('\0');
mem_->StoreBytes(offset, bytes);
offset += bytes.size();
if (offset % 8 != 0) {
offset += 8 - (offset % 8);
}
return ptr;
};
// 'js' is requried as the first argument.
std::vector<std::string> margs = args;
if (margs.size() == 0) {
margs.push_back("js");
} else {
margs[0] = "js";
}
int argc = margs.size();
std::vector<int32_t> argv_ptrs;
for (const std::string& arg : margs) {
argv_ptrs.push_back(str_ptr(arg));
}
argv_ptrs.push_back(0);
// TODO: Add environment variables.
argv_ptrs.push_back(0);
int32_t argv = offset;
for (int32_t ptr : argv_ptrs) {
mem_->StoreInt32(offset, ptr);
mem_->StoreInt32(offset + 4, 0);
offset += 8;
}
inst_->run(argc, argv);
while (!exited_) {
TaskQueue::Task task = task_queue_.Dequeue();
task();
GC();
}
return static_cast<int>(exit_code_);
}
Go::ImportImpl::ImportImpl(Go* go)
: go_{go} {
}
{{range $value := .ImportFuncs}}{{$value.CppImpl "Go::ImportImpl" ""}}
{{end}}
Go::GoObject::GoObject(Go* go)
: go_(go) {
}
Value Go::GoObject::Get(const std::string& key) {
if (key == "_makeFuncWrapper") {
if (!func_make_func_wrapper_.IsFunction()) {
func_make_func_wrapper_ = Value{std::make_shared<Function>(
[this](Value self, std::vector<Value> args) -> Value {
return go_->MakeFuncWrapper(static_cast<int32_t>(args[0].ToNumber()));
}
)};
}
return func_make_func_wrapper_;
}
if (key == "_pendingEvent") {
return go_->pending_event_;
}
error("Go::GoObject::Get: key not found: " + key);
return Value{};
}
void Go::GoObject::Set(const std::string& key, Value value) {
if (key == "_pendingEvent") {
go_->pending_event_ = value;
return;
}
error("key not found: " + key);
}
Value Go::LoadValue(int32_t addr) {
double f = mem_->LoadFloat64(addr);
if (f == 0) {
return Value{};
}
if (!std::isnan(f)) {
return Value{f};
}
int32_t id = static_cast<int32_t>(mem_->LoadUint32(addr));
return values_[id];
}
void Go::StoreValue(int32_t addr, Value v) {
static const int32_t kNaNHead = 0x7FF80000;
if (v.IsNumber() && v.ToNumber() != 0.0) {
double n = v.ToNumber();
if (std::isnan(n)) {
mem_->StoreInt32(addr + 4, kNaNHead);
mem_->StoreInt32(addr, 0);
return;
}
mem_->StoreFloat64(addr, n);
return;
}
if (v.IsUndefined()) {
mem_->StoreFloat64(addr, 0);
return;
}
int32_t id = GetIdFromValue(v);
go_ref_counts_[id]++;
int32_t type_flag = 0;
if (v.IsString()) {
type_flag = 2;
// There is no counterpart for Symbol in C++, then type_flag = 3 is not used.
} else if (v.IsObject() && v.ToObject().IsFunction()) {
type_flag = 4;
} else if (!v.IsNull() && !v.IsNumber() && !v.IsBool()) {
type_flag = 1;
}
mem_->StoreInt32(addr + 4, kNaNHead | type_flag);
mem_->StoreInt32(addr, id);
}
std::vector<Value> Go::LoadSliceOfValues(int32_t addr) {
int32_t array = static_cast<int32_t>(mem_->LoadInt64(addr));
int32_t len = static_cast<int32_t>(mem_->LoadInt64(addr + 8));
std::vector<Value> a(len);
for (int32_t i = 0; i < len; i++) {
a[i] = LoadValue(array + i * 8);
}
return a;
}
void Go::Exit(int32_t code) {
exit_code_ = code;
}
void Go::Resume() {
if (exited_) {
error("Go program has already exited");
}
inst_->resume();
// In wasm_exec.js, |exitPromise| is resolved.
// In this C++, the loop automatically ends when |exited_| is true.
}
Value Go::MakeFuncWrapper(int32_t id) {
// empty_args is a Value of an empty array for arguments.
// This assumes that the argment array is never modified in the callbacks.
// By using the same Value, this can avoid being finalized at syscall/js.finalizeRef.
static Value empty_args = Value{std::vector<Value>()};
static constexpr double inf = std::numeric_limits<double>::infinity();
go_ref_counts_[GetIdFromValue(empty_args)] = inf;
// evt represents the next function to be called when resuming.
// As resuming never happens recursively, this value should be reusable.
static Value evt = Value{std::make_shared<DictionaryValues>(std::map<std::string, Value>{
{"id", Value{0.0}},
{"this", Value{}},
{"args", Value{}},
})};
go_ref_counts_[GetIdFromValue(evt)] = inf;
return Value{std::make_shared<Function>(
[this, id](Value self, std::vector<Value> args) -> Value {
Value argsv;
if (args.size()) {
argsv = Value{args};
} else {
argsv = empty_args;
}
evt.ToObject().Set("id", Value{static_cast<double>(id)});
evt.ToObject().Set("this", self);
evt.ToObject().Set("args", argsv);
pending_event_ = evt;
Resume();
// After Resume is called, pending_event_ should be null.
return Value::ReflectGet(evt, "result");
}
)};
}
void Go::DebugWrite(BytesSpan bytes) {
debug_writer_->Write(std::vector<uint8_t>(bytes.begin(), bytes.end()));
}
int64_t Go::PreciseNowInNanoseconds() {
std::chrono::nanoseconds duration = std::chrono::high_resolution_clock::now() - start_time_point_;
return duration.count();
}
double Go::UnixNowInMilliseconds() {
std::chrono::milliseconds now =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
return now.count();
}
int32_t Go::SetTimeout(double interval) {
int32_t id = next_callback_timeout_id_;
next_callback_timeout_id_++;
std::unique_ptr<Timer> timer = std::make_unique<Timer>(
[this, id] {
task_queue_.Enqueue([this, id]{
Resume();
while (scheduled_timeouts_.find(id) != scheduled_timeouts_.end()) {
// for some reason Go failed to register the timeout event, log and try again
// (temporary workaround for https://github.com/golang/go/issues/28975)
Resume();
}
});
}, interval);
scheduled_timeouts_[id] = std::move(timer);
return id;
}
void Go::ClearTimeout(int32_t id) {
scheduled_timeouts_.erase(id);
}
void Go::GetRandomBytes(BytesSpan bytes) {
// TODO: Use cryptographically strong random values instead of std::random_device.
static std::random_device rd;
std::uniform_int_distribution<uint8_t> dist(0, 255);
for (int i = 0; i < bytes.size(); i++) {
bytes[i] = dist(rd);