question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

context.call: does not support non-global function names

See original GitHub issue

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.new
context.eval('bare = () => 1')
# => #<MiniRacer::JavaScriptFunction:0x00007f96e8acf798>
context.call('bare')
# => 1

This breaks:

context = MiniRacer::Context.new
context.eval('object = {}')
context.eval('object.nested = () => 1')
# => #<MiniRacer::JavaScriptFunction:0x00007f96e899d5a0>
context.call('object.nested')
# => Unknown JavaScript method invoked (MiniRacer::RuntimeError)

Environment:

  • ruby: 3.0.2
  • mini_racer: 0.5.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
tisbacommented, Feb 14, 2022

That’s a good point, @gi, thx!

For reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const:

Constants are block-scoped, much like variables declared using the let keyword.[…]

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 foo in subsequent #eval calls, it looks like the same scope is used every time. I think these issues are related after all and it feels like a similar lookup issue 🤔

0reactions
gicommented, Feb 14, 2022

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"]()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I set a (non-global) variable in a jinja context function?
I have many situations where I don't have just one but up to 20 variables that are being set and then a macro...
Read more >
26. Global vs. Local Variables and Namespaces
So, we would have s both as a global and a local variable in the same scope, i.e. the body of the function....
Read more >
UpdateContext function in Power Apps - Microsoft Learn
Context variables are implicitly created by using the UpdateContext or Navigate function. There is no explicit declaration required.
Read more >
482 Function call blocks and local variable/function names
Function call parameters are local to the function and are not injected into the calling context. That is, given a definition of foo(x)=......
Read more >
C Programming Course Notes - Functions
Any variable changes made within a function are local to that function. A calling function's variables are not affected by the actions of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found