Skip to content

passing ruby objects

sergeych edited this page Jan 22, 2015 · 4 revisions

Just pass any ruby object you want.

The default security model allow javascript code to access all public methods above Object’s (Kernel’s). These are unsafe to call. You can proxy those you need by overriding. The simplest way is to set the object in the context namespace:

cxt = H8::Context.new
cxt[:instance] = MyClass.new
cxt.eval 'instance.method(1,2);'

This will work. The same, you can return callable from JS and then call it with ruby arguments:

callable = cxt.eval "function(a,b) { ... } "
callable.call "test", MyClass.new

While simple values are converted to the corresponding Javascript primitive types, complex objects are gated intact. Take care: H8 does not converts hashes or arrays!. All Gated objects are instanceof RubyGate:

cxt.eval 'instance instanceof RubyGate' # => true

You can also gate while ruby classes that will become javascript constructors. See ruby class gate.

The conversion between some important primitive types and constants is:

ruby js
nil null
H8::Undefined undefined
false false
true true

Id map of gated objects

H8 makes sure that the same objects in ruby produces same wrapped objects in javascript – when you^ for example, return self from ruby, it will provide the same object in the javascript side (you can check it with ===). If you ever wrapped ruby object, it will always have the same javascript side object.

Inverse is not guaranteed. If you need the wrapped javascript on the ruby side maintain identity across calls to javascript code, please check it first and make an issue (worse) or a PR (good).

To write: how to relax security model

Clone this wiki locally