Skip to content

Add syscall/js #908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 30, 2019
Merged

Add syscall/js #908

merged 9 commits into from
Apr 30, 2019

Conversation

hajimehoshi
Copy link
Member

This PR adds a new package syscall/js. Originally syscall/js is for WebAssembly, but in GopherJS this package works as a wrapper of github.com/gopherjs/gopherjs/js. With this package, developers can use syscall/js both for GopherJS and WebAssembly.

The implementation is basically same as github.com/gohperjs/gopherwasm.

Fixes #899

@hajimehoshi
Copy link
Member Author

--- FAIL: TestSleep (0.10s)
    test.148654449:38: Sleep(100ms) slept for only 99ms

Meh, this sounds like a flaky issue

@neelance
Copy link
Member

neelance commented Apr 6, 2019

@hajimehoshi While working on WebAssembly I've noticed that setTimeout sometimes invokes the callback 1ms early. Maybe this is the same issue. It would probably be okay to always add 1ms to the delay.

@hajimehoshi
Copy link
Member Author

@neelance Thank. I think I'll do this in another PR later.

Copy link
Member

@dmitshur dmitshur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this PR look good to me.

See inline comments. Hopefully the flaky test can be worked around so the tests pass before this is merged.

@hajimehoshi
Copy link
Member Author

@neelance Hi, are you happy with this PR? Thanks!

@hajimehoshi
Copy link
Member Author

ping @neelance

@neelance
Copy link
Member

Is this passing the normal tests of the syscall/js package?https://github.com/golang/go/blob/master/src/syscall/js/js_test.go

Is there any particular need for the custom test cases of this PR?

@hajimehoshi
Copy link
Member Author

To test js_test.go, we'd need to copy the entire file except for // +build js,wasm. Should we do that?

@hajimehoshi
Copy link
Member Author

BTW, I put a test file at compiler/natives/src/syscall/js, but the test was not executed, I'm not sure the reason though.

@neelance
Copy link
Member

Shouldn't it be possible to modify importWithSrcDir to manually include js_test.go in the build?

@hajimehoshi
Copy link
Member Author

hajimehoshi commented Apr 24, 2019

Almost done! I found a problem in js_test.go:

func TestIntConversion(t *testing.T) {                                                                                                                                                                                                    
        testIntConversion(t, 0)                                                                                                                                                                                                           
        testIntConversion(t, 1)                                                                                                                                                                                                           
        testIntConversion(t, -1)                                                                                                                                                                                                          
        testIntConversion(t, 1<<20)                                                                                                                                                                                                       
        testIntConversion(t, -1<<20)                                                                                                                                                                                                      
        testIntConversion(t, 1<<40)                                                                                                                                                                                                       
        testIntConversion(t, -1<<40)                                                                                                                                                                                                      
        testIntConversion(t, 1<<60)                                                                                                                                                                                                       
        testIntConversion(t, -1<<60)                                                                                                                                                                                                      
}

This cannot be compiled on 32bit-integer environment and GopherJS is an environment with 32bit integers. Replacing the test function in a usual GopherJS way didn't work. Probably the const check happens before the replacing. Any suggestions?

EDIT:

Looks like CircleCI tests passed. No worries!

On my local machine, go generate ./... && go run . test syscall/js with overflow errors, but I might miss something.

EDIT2:

My bad, I didn't add syscall/js for tests explicitly. Now CircleCI says the same error as mine:

../../../../../../../usr/local/go/src/syscall/js/js_test.go:106:23: 1 << 40 (untyped int constant 1099511627776) overflows int
../../../../../../../usr/local/go/src/syscall/js/js_test.go:107:23: -1 << 40 (untyped int constant -1099511627776) overflows int
../../../../../../../usr/local/go/src/syscall/js/js_test.go:108:23: 1 << 60 (untyped int constant 1152921504606846976) overflows int
../../../../../../../usr/local/go/src/syscall/js/js_test.go:109:23: -1 << 60 (untyped int constant -1152921504606846976) overflows int
Exited with code 1

@neelance
Copy link
Member

This is unfortunate. Okay, maybe coping js_test.go is the simplest solution.

@hajimehoshi
Copy link
Member Author

OK, I copied the test, but now I'm facing a new error on my machine:

$ go generate ./... && go run . test syscall/js
writing fs_vfsdata.go
writing fs_vfsdata.go
gopherjs: Source maps disabled. Install source-map-support module for nice stack traces. See https://github.com/gopherjs/gopherjs#gopherjs-run-gopherjs-test.
testing: warning: no tests to run
PASS
[...]

Looks like the file is compiled Now it's midnight, I'll investigate this further later...

@hajimehoshi
Copy link
Member Author

@neelance In the current implementation, is it possible to 'add' new tests in compiler/native?

@hajimehoshi
Copy link
Member Author

is it possible to 'add' new tests in compiler/native?

I thought it impossible, so I moved js_test.go in compiler/native to tests/syscalljs/js_test.go. Also I found some tests that cannot pass, so I added t.Skip.

  • TestNaN: comparing NaN doesn't work with *js.Object
  • TestZeroValue: the initial value of *js.Object is nil, which is not undefined.

Please take a look

@neelance
Copy link
Member

The zero value is quite important. I think it should be possible to handle this correctly, shouldn't it?

@hajimehoshi
Copy link
Member Author

I think it should be possible to handle this correctly, shouldn't it?

Yes. I fixed that so that the zero value is js.Undefined.

I appreciate your patience and your support. Please take another look.

@hajimehoshi
Copy link
Member Author

ping

@hajimehoshi hajimehoshi merged commit de1776f into master Apr 30, 2019
@hajimehoshi hajimehoshi deleted the syscalljs branch April 30, 2019 15:56
@johanbrandhorst
Copy link

This is fantastic 🎉. Well done @hajimehoshi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unify GopherJS's syscall/js and GopherWasm
4 participants