mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Merge pull request #77 from judofyr/clarify-call
Improved documentation/tests for exec/eval/call
This commit is contained in:
commit
7dd2a79676
4 changed files with 47 additions and 3 deletions
|
@ -26,7 +26,8 @@ module ExecJS
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(identifier, *args)
|
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
|
rescue Exception => e
|
||||||
raise wrap_error(e)
|
raise wrap_error(e)
|
||||||
end
|
end
|
||||||
|
|
|
@ -42,7 +42,7 @@ module ExecJS
|
||||||
def call(properties, *args)
|
def call(properties, *args)
|
||||||
lock do
|
lock do
|
||||||
begin
|
begin
|
||||||
unbox @v8_context.eval(properties).call(*args)
|
unbox @v8_context.eval("(#{properties})").call(*args)
|
||||||
rescue ::V8::JSError => e
|
rescue ::V8::JSError => e
|
||||||
raise wrap_error(e)
|
raise wrap_error(e)
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,15 +9,30 @@ module ExecJS
|
||||||
def initialize(runtime, source = "", options = {})
|
def initialize(runtime, source = "", options = {})
|
||||||
end
|
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 = {})
|
def exec(source, options = {})
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
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 = {})
|
def eval(source, options = {})
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
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
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -48,6 +48,34 @@ class TestExecJS < Test
|
||||||
assert_equal "bar", context.call("a.b.id", "bar")
|
assert_equal "bar", context.call("a.b.id", "bar")
|
||||||
end
|
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
|
def test_context_call_missing_function
|
||||||
context = ExecJS.compile("")
|
context = ExecJS.compile("")
|
||||||
assert_raises ExecJS::ProgramError do
|
assert_raises ExecJS::ProgramError do
|
||||||
|
|
Loading…
Reference in a new issue