Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 7b85d0d

Browse files
committed
add additional examples of accessing JavaScript values
1 parent 59508cc commit 7b85d0d

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,20 @@ evaluate some simple JavaScript
3131
cxt = V8::Context.new
3232
cxt.eval('7 * 6') #=> 42
3333

34-
call JavaScript functions
34+
access values inside your JavaScript context from Ruby
35+
36+
cxt.eval 'var val = {num: 5, fun: function isTruthy(arg) { return !!arg }}'
37+
val = cxt[:val] #=> V8::Object
38+
cxt[:val] == cxt.scope.val #=> true
39+
val.num #=> 5
40+
val.isTruthy(1) #=> true
41+
42+
this includes references to JavaScript functions
43+
44+
truthy = val[:isTruthy] #=> V8::Function
45+
truthy.call(' ') #=> true
46+
truthy.call(0) #=> false
3547

36-
cxt.eval "function isTruthy(arg) { return !!arg }"
37-
cxt[:isTruthy].call(' ') #=> true
38-
cxt.scope.isTruthy(0) #=> false
3948

4049
embed values into the scope of your context
4150

0 commit comments

Comments
 (0)