| 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 |
// indexByte is copied from bytes package to avoid importing it (since the real syscall package doesn't). |
| 53 |
func indexByte(s []byte, c byte) int { |
| 54 |
for i, b := range s { |
| 55 |
if b == c { |
| 56 |
return i |
| 57 |
} |
| 58 |
} |
| 59 |
return -1 |
| 60 |
} |