-
Notifications
You must be signed in to change notification settings - Fork 93
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
context.call: does not support non-global function names #224
Comments
Nice catch, I do support fixing this, would you like to try a PR? I assume
we need to be a bit more sophisticated around looking up the function.
…On Wed, Jan 5, 2022 at 5:56 AM Zach Gianos ***@***.***> wrote:
When calling a function, only global function names are supported.
Functions which are properties of an object, or otherwise nested/scoped,
are not supported: e.g. object.fn.
This works:
context = MiniRacer::Context.newcontext.eval('bare = () => 1')# => #<MiniRacer::JavaScriptFunction:0x00007f96e8acf798>context.call('bare')# => 1
This breaks:
context = MiniRacer::Context.newcontext.eval('object = {}')context.eval('object.nested = () => 1')# => #<MiniRacer::JavaScriptFunction:0x00007f96e899d5a0>context.call('object.nested')# => Unknown JavaScript method invoked (MiniRacer::RuntimeError)
—
Reply to this email directly, view it on GitHub
<#224>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAABIXPXSSWXNSPX2GPA25LUUM7FNANCNFSM5LIEHVYA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
While looking at #136, I noticed, that MiniRacer cannot invoke functions that are defined with require "mini_racer"
require "date"
ctx = MiniRacer::Context.new
ctx.eval("const echoFn = (obj) => obj;")
ctx.call("echoFn", "hello world!")
# => /Users/basti/.rvm/gems/ruby-3.1.0/gems/mini_racer-0.6.2/lib/mini_racer.rb:237:in `call_unsafe': Unknown JavaScript method invoked (MiniRacer::RuntimeError) |
@tisba That is a different issue and may be by design. As you may know, |
The original issue here is that the string used to identify the function to call is not evaluated to lookup the function: In Ruby, context.call("a.b") is interpreted in JavaScript as this["a.b"]() instead of this["a"]["b"]() |
That's a good point, @gi, thx! For reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const:
But what surprises me then is that the defined constant is still visible on subsequent invocations: require "mini_racer"
ctx = MiniRacer::Context.new
ctx.eval("const foo = 42;")
a = ctx.eval("foo + 1;")
# a => 43
ctx.eval("foo = 42;")
# JavaScript at <anonymous>:1:5: TypeError: Assignment to constant variable. (MiniRacer::RuntimeError) Since I can also use |
When calling a function, only global function names are supported. Functions which are properties of an object, or otherwise nested/scoped, are not supported: e.g.
object.fn
.This works:
This breaks:
Environment:
3.0.2
0.5.0
The text was updated successfully, but these errors were encountered: