Repository navigation
Expand file tree
/
Copy pathconsole.go
More file actions
53 lines (49 loc) · 1.48 KB
/
Copy pathconsole.go
File metadata and controls
53 lines (49 loc) · 1.48 KB
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
package ramune
import (
"fmt"
"io"
"strings"
)
// installConsole registers console.log/error/warn/info/debug backed by
// the Runtime's stdout/stderr writers. Called unconditionally during
// Runtime initialization (before NodeCompat).
func (r *Runtime) installConsole() error {
if err := r.registerFuncLocked("__go_stdout", r.printFunc(r.stdout)); err != nil {
return err
}
if err := r.registerFuncLocked("__go_stderr", r.printFunc(r.stderr)); err != nil {
return err
}
if err := r.registerFuncLocked("__go_stdout_raw", r.rawWriteFunc(r.stdout)); err != nil {
return err
}
if err := r.registerFuncLocked("__go_stderr_raw", r.rawWriteFunc(r.stderr)); err != nil {
return err
}
return r.execLocked(`
if (typeof globalThis.console === 'undefined') globalThis.console = {};
console.log = function() { __go_stdout.apply(null, Array.prototype.slice.call(arguments)); };
console.info = console.log;
console.debug = console.log;
console.error = function() { __go_stderr.apply(null, Array.prototype.slice.call(arguments)); };
console.warn = console.error;
`)
}
func (r *Runtime) printFunc(w io.Writer) GoFunc {
return func(args []any) (any, error) {
parts := make([]string, len(args))
for i, a := range args {
parts[i] = fmt.Sprint(a)
}
fmt.Fprintln(w, strings.Join(parts, " "))
return nil, nil
}
}
func (r *Runtime) rawWriteFunc(w io.Writer) GoFunc {
return func(args []any) (any, error) {
if len(args) > 0 {
fmt.Fprint(w, args[0])
}
return true, nil
}
}