Skip to content

Commit 4bef9b3

Browse files
committed
Expose Opal.bridge_class for bridging native prototypes
1 parent 77e8175 commit 4bef9b3

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

corelib/runtime.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@
176176

177177
constructor.prototype.constructor = constructor;
178178

179-
// class itself
179+
return boot_class_meta(superklass, constructor);
180+
};
181+
182+
// class itself
183+
function boot_class_meta(superklass, constructor) {
180184
var mtor = function() {};
181185
mtor.prototype = superklass.constructor.prototype;
182186

@@ -198,7 +202,7 @@
198202
constructor.prototype._klass = klass;
199203

200204
return klass;
201-
};
205+
}
202206

203207
// Define new module (or return existing module)
204208
Opal.module = function(base, id) {
@@ -323,16 +327,26 @@
323327
* @return [Class] returns new ruby class
324328
*/
325329
function bridge_class(name, constructor) {
326-
var klass = boot_class(RubyObject, constructor);
330+
var klass = boot_class_meta(RubyObject, constructor);
327331

328332
klass._name = name;
329333

330334
create_scope(Opal, klass, name);
331335
bridged_classes.push(klass);
332336

337+
var object_methods = RubyBasicObject._methods.concat(RubyObject._methods);
338+
339+
for (var i = 0, len = object_methods.length; i < len; i++) {
340+
var meth = object_methods[i];
341+
constructor.prototype[meth] = RubyObject._proto[meth];
342+
}
343+
333344
return klass;
334345
};
335346

347+
// private method
348+
Opal.bridge_class = bridge_class;
349+
336350
/*
337351
* constant assign
338352
*/
@@ -714,6 +728,7 @@
714728
};
715729

716730
function define_basic_object_method(jsid, body) {
731+
RubyBasicObject._methods.push(jsid);
717732
for (var i = 0, len = bridged_classes.length; i < len; i++) {
718733
bridged_classes[i]._proto[jsid] = body;
719734
}

spec/corelib/runtime/bridged_classes_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
require 'spec_helper'
22

3+
%x{
4+
var bridge_class_demo = function(){};
5+
Opal.bridge_class('TopBridgedClassDemo', bridge_class_demo);
6+
bridge_class_demo.prototype.$foo = function() { return "bar" };
7+
}
8+
39
describe "Bridged Classes" do
10+
describe "Opal.bridge_class" do
11+
before do
12+
@bridged = ::TopBridgedClassDemo
13+
@instance = `new bridge_class_demo`
14+
end
15+
16+
it "should expose the given class at the top level scope" do
17+
@bridged.should be_kind_of(Class)
18+
end
19+
20+
it "gives the class the correct name" do
21+
@bridged.name.should == "TopBridgedClassDemo"
22+
end
23+
24+
it "should have all BasicObject methods defined" do
25+
@instance.should respond_to(:instance_eval)
26+
@bridged.new.should respond_to(:==)
27+
end
28+
29+
it "should have all Object methods defined" do
30+
@instance.should respond_to(:class)
31+
@bridged.new.should respond_to(:singleton_class)
32+
end
33+
34+
it "instances of class should be able to call native ruby methods" do
35+
@instance.foo.should == "bar"
36+
@bridged.new.foo.should == "bar"
37+
end
38+
end
39+
440
describe ".instance_methdods" do
541
it "should report methods for class" do
642
Array.instance_methods.should include(:shift)

0 commit comments

Comments
 (0)