diff --git a/bridge.go b/bridge.go index f114212..f4cd989 100644 --- a/bridge.go +++ b/bridge.go @@ -7,6 +7,7 @@ import ( "reflect" "sync" "syscall" + "time" "unsafe" "github.com/wasmerio/go-ext-wasm/wasmer" @@ -102,6 +103,19 @@ func (b *Bridge) addValues() { "Float32Array": typedArray("Float32Array"), "Float64Array": typedArray("Float64Array"), "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{}{ "constants": propObject("constants", map[string]interface{}{ "O_WRONLY": syscall.O_WRONLY, diff --git a/imports.go b/imports.go index fae6ea1..95ed0cd 100644 --- a/imports.go +++ b/imports.go @@ -67,10 +67,9 @@ func nanotime(ctx unsafe.Pointer, sp int32) { //export walltime func walltime(ctx unsafe.Pointer, sp int32) { b := getBridge(ctx) - t := time.Now().Unix() - sec := t / 1000 - nanos := (t % 1000) * 1000000 - b.setInt64(sp+8, sec) + t := time.Now().UnixNano() + nanos := t % 1000000000 + b.setInt64(sp+8, t/1000000000) b.setInt32(sp+16, int32(nanos)) } diff --git a/simple/caller/main.go b/simple/caller/main.go index a785dcc..d54a4ac 100644 --- a/simple/caller/main.go +++ b/simple/caller/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" wasmgo "github.com/vedhavyas/wasm" @@ -9,7 +8,7 @@ import ( func proxy(b *wasmgo.Bridge) wasmgo.Func { return func(args []interface{}) (i interface{}, e error) { - fmt.Println("In Go", args) + log.Println("In Go", args) return b.CallFunc("addition", args) } } @@ -33,5 +32,5 @@ func main() { } <-done - fmt.Println("wasm exited", err) + log.Println("wasm exited", err) } diff --git a/simple/prog/main.go b/simple/prog/main.go index 3622521..1a9043d 100644 --- a/simple/prog/main.go +++ b/simple/prog/main.go @@ -3,13 +3,13 @@ package main import ( - "fmt" + "log" "syscall/js" ) // TODO: log seems to cause an issue 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() return a + b } @@ -22,6 +22,6 @@ func main() { js.Global().Set("addition", fun) 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 } diff --git a/simple/prog/main.wasm b/simple/prog/main.wasm index e04124f..61dbb9d 100755 Binary files a/simple/prog/main.wasm and b/simple/prog/main.wasm differ