fix walltimne

pull/2/head
vedhavyas 2019-08-20 13:47:40 -07:00
parent 4f9c552ed7
commit 26ad7c8f0b
No known key found for this signature in database
GPG Key ID: 317BF0923E3EB7E5
5 changed files with 22 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import (
"reflect" "reflect"
"sync" "sync"
"syscall" "syscall"
"time"
"unsafe" "unsafe"
"github.com/wasmerio/go-ext-wasm/wasmer" "github.com/wasmerio/go-ext-wasm/wasmer"
@ -102,6 +103,19 @@ func (b *Bridge) addValues() {
"Float32Array": typedArray("Float32Array"), "Float32Array": typedArray("Float32Array"),
"Float64Array": typedArray("Float64Array"), "Float64Array": typedArray("Float64Array"),
"process": propObject("process", nil), "process": propObject("process", nil),
"Date": &object{name: "Date", new: func(args []interface{}) interface{} {
t := time.Now()
return &object{name: "DateInner", props: map[string]interface{}{
"time": t,
"getTimezoneOffset": Func(func(args []interface{}) (interface{}, error) {
_, offset := t.Zone()
// make it negative and return in minutes
offset = (offset / 60) * -1
return offset, nil
}),
}}
}},
"fs": propObject("fs", map[string]interface{}{ "fs": propObject("fs", map[string]interface{}{
"constants": propObject("constants", map[string]interface{}{ "constants": propObject("constants", map[string]interface{}{
"O_WRONLY": syscall.O_WRONLY, "O_WRONLY": syscall.O_WRONLY,

View File

@ -67,10 +67,9 @@ func nanotime(ctx unsafe.Pointer, sp int32) {
//export walltime //export walltime
func walltime(ctx unsafe.Pointer, sp int32) { func walltime(ctx unsafe.Pointer, sp int32) {
b := getBridge(ctx) b := getBridge(ctx)
t := time.Now().Unix() t := time.Now().UnixNano()
sec := t / 1000 nanos := t % 1000000000
nanos := (t % 1000) * 1000000 b.setInt64(sp+8, t/1000000000)
b.setInt64(sp+8, sec)
b.setInt32(sp+16, int32(nanos)) b.setInt32(sp+16, int32(nanos))
} }

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"log" "log"
wasmgo "github.com/vedhavyas/wasm" wasmgo "github.com/vedhavyas/wasm"
@ -9,7 +8,7 @@ import (
func proxy(b *wasmgo.Bridge) wasmgo.Func { func proxy(b *wasmgo.Bridge) wasmgo.Func {
return func(args []interface{}) (i interface{}, e error) { return func(args []interface{}) (i interface{}, e error) {
fmt.Println("In Go", args) log.Println("In Go", args)
return b.CallFunc("addition", args) return b.CallFunc("addition", args)
} }
} }
@ -33,5 +32,5 @@ func main() {
} }
<-done <-done
fmt.Println("wasm exited", err) log.Println("wasm exited", err)
} }

View File

@ -3,13 +3,13 @@
package main package main
import ( import (
"fmt" "log"
"syscall/js" "syscall/js"
) )
// TODO: log seems to cause an issue // TODO: log seems to cause an issue
func addition(this js.Value, args []js.Value) interface{} { func addition(this js.Value, args []js.Value) interface{} {
fmt.Println("In WASM", args) log.Println("In WASM", args)
a, b := args[0].Int(), args[1].Int() a, b := args[0].Int(), args[1].Int()
return a + b return a + b
} }
@ -22,6 +22,6 @@ func main() {
js.Global().Set("addition", fun) js.Global().Set("addition", fun)
res := js.Global().Get("proxy").Invoke(1, 2) res := js.Global().Get("proxy").Invoke(1, 2)
fmt.Printf("1 + 2 = %d\n", res.Int()) log.Printf("1 + 2 = %d\n", res.Int())
<-ch <-ch
} }

Binary file not shown.