Skip to content

Commit

Permalink
Merge pull request #77 from judofyr/clarify-call
Browse files Browse the repository at this point in the history
Improved documentation/tests for exec/eval/call
  • Loading branch information
rafaelfranca committed Sep 20, 2018
2 parents 5f78865 + 412b1e8 commit 7dd2a79
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/execjs/duktape_runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def eval(source, options = {})
end

def call(identifier, *args)
@ctx.call_prop(identifier.split("."), *args)
@ctx.exec_string("__execjs_duktape_call = #{identifier}", '(execjs)')
@ctx.call_prop("__execjs_duktape_call", *args)
rescue Exception => e
raise wrap_error(e)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/execjs/ruby_racer_runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def eval(source, options = {})
def call(properties, *args)
lock do
begin
unbox @v8_context.eval(properties).call(*args)
unbox @v8_context.eval("(#{properties})").call(*args)
rescue ::V8::JSError => e
raise wrap_error(e)
end
Expand Down
17 changes: 16 additions & 1 deletion lib/execjs/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,30 @@ class Context
def initialize(runtime, source = "", options = {})
end

# Evaluates the +source+ in the context of a function body and returns the
# returned value.
#
# context.exec("return 1") # => 1
# context.exec("1") # => nil (nothing was returned)
def exec(source, options = {})
raise NotImplementedError
end

# Evaluates the +source+ as an expression and returns the result.
#
# context.eval("1") # => 1
# context.eval("return 1") # => Raises SyntaxError
def eval(source, options = {})
raise NotImplementedError
end

def call(properties, *args)
# Evaluates +source+ as an expression (which should be of type
# +function+), and calls the function with the given arguments.
# The function will be evaluated with the global object as +this+.
#
# context.call("function(a, b) { return a + b }", 1, 1) # => 2
# context.call("CoffeeScript.compile", "1 + 1")
def call(source, *args)
raise NotImplementedError
end
end
Expand Down
28 changes: 28 additions & 0 deletions test/test_execjs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ def test_nested_context_call
assert_equal "bar", context.call("a.b.id", "bar")
end

def test_call_with_complex_properties
context = ExecJS.compile("")
assert_equal 2, context.call("function(a, b) { return a + b }", 1, 1)

context = ExecJS.compile("foo = 1")
assert_equal 2, context.call("(function(bar) { return foo + bar })", 1)
end

def test_call_with_this
# Known bug: https://github.com/cowboyd/therubyrhino/issues/39
skip if ExecJS.runtime.is_a?(ExecJS::RubyRhinoRuntime)

# Make sure that `this` is indeed the global scope
context = ExecJS.compile(<<-EOF)
name = 123;
function Person(name) {
this.name = name;
}
Person.prototype.getThis = function() {
return this.name;
}
EOF

assert_equal 123, context.call("(new Person('Bob')).getThis")
end

def test_context_call_missing_function
context = ExecJS.compile("")
assert_raises ExecJS::ProgramError do
Expand Down

0 comments on commit 7dd2a79

Please sign in to comment.