github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/compiler/natives/src/syscall/syscall.go (about) 1 // +build js 2 3 package syscall 4 5 import ( 6 "unsafe" 7 8 "github.com/gopherjs/gopherjs/js" 9 ) 10 11 var warningPrinted = false 12 var lineBuffer []byte 13 14 func init() { 15 js.Global.Set("$flushConsole", js.InternalObject(func() { 16 if len(lineBuffer) != 0 { 17 js.Global.Get("console").Call("log", string(lineBuffer)) 18 lineBuffer = nil 19 } 20 })) 21 } 22 23 func printWarning() { 24 if !warningPrinted { 25 js.Global.Get("console").Call("error", "warning: system calls not available, see https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md") 26 } 27 warningPrinted = true 28 } 29 30 func printToConsole(b []byte) { 31 goPrintToConsole := js.Global.Get("goPrintToConsole") 32 if goPrintToConsole != js.Undefined { 33 goPrintToConsole.Invoke(js.InternalObject(b)) 34 return 35 } 36 37 lineBuffer = append(lineBuffer, b...) 38 for { 39 i := indexByte(lineBuffer, '\n') 40 if i == -1 { 41 break 42 } 43 js.Global.Get("console").Call("log", string(lineBuffer[:i])) // don't use println, since it does not externalize multibyte characters 44 lineBuffer = lineBuffer[i+1:] 45 } 46 } 47 48 func use(p unsafe.Pointer) { 49 // no-op 50 } 51 52 func Exit(code int) { 53 Syscall(exitTrap, uintptr(code), 0, 0) 54 } 55 56 // indexByte is copied from bytes package to avoid importing it (since the real syscall package doesn't). 57 func indexByte(s []byte, c byte) int { 58 for i, b := range s { 59 if b == c { 60 return i 61 } 62 } 63 return -1 64 }