Skip to content

Commit 7dd2a79

Browse files
authored
Merge pull request #77 from judofyr/clarify-call
Improved documentation/tests for exec/eval/call
2 parents 5f78865 + 412b1e8 commit 7dd2a79

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

lib/execjs/duktape_runtime.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def eval(source, options = {})
2626
end
2727

2828
def call(identifier, *args)
29-
@ctx.call_prop(identifier.split("."), *args)
29+
@ctx.exec_string("__execjs_duktape_call = #{identifier}", '(execjs)')
30+
@ctx.call_prop("__execjs_duktape_call", *args)
3031
rescue Exception => e
3132
raise wrap_error(e)
3233
end

lib/execjs/ruby_racer_runtime.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def eval(source, options = {})
4242
def call(properties, *args)
4343
lock do
4444
begin
45-
unbox @v8_context.eval(properties).call(*args)
45+
unbox @v8_context.eval("(#{properties})").call(*args)
4646
rescue ::V8::JSError => e
4747
raise wrap_error(e)
4848
end

lib/execjs/runtime.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,30 @@ class Context
99
def initialize(runtime, source = "", options = {})
1010
end
1111

12+
# Evaluates the +source+ in the context of a function body and returns the
13+
# returned value.
14+
#
15+
# context.exec("return 1") # => 1
16+
# context.exec("1") # => nil (nothing was returned)
1217
def exec(source, options = {})
1318
raise NotImplementedError
1419
end
1520

21+
# Evaluates the +source+ as an expression and returns the result.
22+
#
23+
# context.eval("1") # => 1
24+
# context.eval("return 1") # => Raises SyntaxError
1625
def eval(source, options = {})
1726
raise NotImplementedError
1827
end
1928

20-
def call(properties, *args)
29+
# Evaluates +source+ as an expression (which should be of type
30+
# +function+), and calls the function with the given arguments.
31+
# The function will be evaluated with the global object as +this+.
32+
#
33+
# context.call("function(a, b) { return a + b }", 1, 1) # => 2
34+
# context.call("CoffeeScript.compile", "1 + 1")
35+
def call(source, *args)
2136
raise NotImplementedError
2237
end
2338
end

test/test_execjs.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ def test_nested_context_call
4848
assert_equal "bar", context.call("a.b.id", "bar")
4949
end
5050

51+
def test_call_with_complex_properties
52+
context = ExecJS.compile("")
53+
assert_equal 2, context.call("function(a, b) { return a + b }", 1, 1)
54+
55+
context = ExecJS.compile("foo = 1")
56+
assert_equal 2, context.call("(function(bar) { return foo + bar })", 1)
57+
end
58+
59+
def test_call_with_this
60+
# Known bug: https://github.com/cowboyd/therubyrhino/issues/39
61+
skip if ExecJS.runtime.is_a?(ExecJS::RubyRhinoRuntime)
62+
63+
# Make sure that `this` is indeed the global scope
64+
context = ExecJS.compile(<<-EOF)
65+
name = 123;
66+
67+
function Person(name) {
68+
this.name = name;
69+
}
70+
71+
Person.prototype.getThis = function() {
72+
return this.name;
73+
}
74+
EOF
75+
76+
assert_equal 123, context.call("(new Person('Bob')).getThis")
77+
end
78+
5179
def test_context_call_missing_function
5280
context = ExecJS.compile("")
5381
assert_raises ExecJS::ProgramError do

0 commit comments

Comments
 (0)