Skip to content
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

Bridged functions taking goja.Value args misbehave around null values #32

Closed
liclac opened this issue Jun 10, 2017 · 1 comment
Closed
Labels

Comments

@liclac
Copy link

liclac commented Jun 10, 2017

A function that takes a goja.Value argument will, when bridged to JS and invoked with null for that argument, receive a nil goja.Value, which is not recognised as a proper null by goja.IsNull().

Test case:

func TestNullArguments(t *testing.T) {
	rt := goja.New()
	rt.Set("fn", func(v goja.Value) {
		if v == nil {
			t.Error("null becomes nil")
		}
		if !goja.IsNull(v) {
			t.Error("null is not null")
		}
	})
	rt.RunString(`fn(null);`)
}
@dop251 dop251 closed this as completed in d1f4162 Jun 10, 2017
@dop251
Copy link
Owner

dop251 commented Jun 10, 2017

I have fixed this, however if your function already takes goja.Value as an argument you should consider making it a 'native' goja function. This way you avoid reflection wrapper which will improve performance:

func BenchmarkCallReflect(b *testing.B) {
	vm := New()
	vm.Set("f", func(v Value) {

	})

	prg := MustCompile("test.js", "f(null)", true)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		vm.RunProgram(prg)
	}
}

func BenchmarkCallNative(b *testing.B) {
	vm := New()
	vm.Set("f", func(call FunctionCall) (ret Value) {
		return
	})

	prg := MustCompile("test.js", "f(null)", true)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		vm.RunProgram(prg)
	}
}
BenchmarkCallReflect-8   	  300000	      4018 ns/op	     160 B/op	       3 allocs/op
BenchmarkCallNative-8    	 2000000	       598 ns/op	     128 B/op	       2 allocs/op

@dop251 dop251 added the bug label Jun 14, 2017
tsedgwick pushed a commit to tsedgwick/goja that referenced this issue Jun 15, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants