Skip to content

Commit 0469bf8

Browse files
committed
Change Native per opal#347
1 parent 657fa38 commit 0469bf8

9 files changed

Lines changed: 54 additions & 60 deletions

File tree

corelib/kernel.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def Array(object, func = undefined, length = :length)
5959
if (object == null || object === nil) {
6060
return [];
6161
}
62-
else if (#{Native === object} && object[length] != null) {
62+
else if (#{native?(object)} && object[length] != null) {
6363
var result = [];
6464
6565
for (var i = 0, length = object[length]; i < length; i++) {

corelib/native.rb

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,50 @@
11
module Kernel
2+
def native?(value)
3+
`value == null || !value._klass`
4+
end
5+
26
def Native(obj)
3-
if Native === obj
4-
Native::Object.new(obj)
7+
if native?(obj)
8+
Native.new(obj)
59
else
610
obj
711
end
812
end
913
end
1014

1115
class Native
12-
def self.===(value)
13-
if self == Native
14-
`value == null || !value._klass`
15-
else
16-
super
16+
module Base
17+
module Helpers
18+
def alias_native(new, old)
19+
define_method new do |*args|
20+
Native.call(@native, old, *args)
21+
end
22+
end
23+
end
24+
25+
def self.included?(klass)
26+
klass.instance_eval {
27+
extend Helpers
28+
}
29+
end
30+
31+
def initialize(native)
32+
unless native?(native)
33+
raise ArgumentError, "the passed value isn't native"
34+
end
35+
36+
@native = native
37+
end
38+
39+
def to_n
40+
@native
1741
end
1842
end
1943

2044
def self.try_convert(value)
2145
%x{
22-
if (#{self === value}) {
23-
return value.valueOf();
46+
if (#{native?(value)}) {
47+
return #{value}.valueOf();
2448
}
2549
else if (#{value.respond_to? :to_n}) {
2650
return #{value.to_n};
@@ -41,12 +65,6 @@ def self.convert(value)
4165
native
4266
end
4367

44-
def self.alias_native(new, old)
45-
define_method new do |*args|
46-
Native.call(@native, old, *args)
47-
end
48-
end
49-
5068
def self.call(obj, key, *args, &block)
5169
args << block if block
5270

@@ -61,7 +79,7 @@ def self.call(obj, key, *args, &block)
6179
6280
return result == null ? nil : result;
6381
}
64-
else if (#{self === `prop`}) {
82+
else if (#{native?(`prop`)}) {
6583
return #{Native(`prop`)};
6684
}
6785
else {
@@ -70,35 +88,7 @@ def self.call(obj, key, *args, &block)
7088
}
7189
end
7290

73-
def self.new(*)
74-
if self == Native
75-
raise ArgumentError, "cannot instantiate non derived Native"
76-
else
77-
super
78-
end
79-
end
80-
81-
def initialize(native)
82-
@native = Native.convert(native)
83-
end
84-
85-
def to_n
86-
@native
87-
end
88-
end
89-
90-
class Native::Object < BasicObject
91-
def initialize(native)
92-
unless Native === native
93-
raise ArgumentError, "the passed value isn't native"
94-
end
95-
96-
@native = native
97-
end
98-
99-
def to_n
100-
@native
101-
end
91+
include Base
10292

10393
def nil?
10494
`#@native == null`

corelib/struct.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def self.members
4040
include Enumerable
4141

4242
def initialize(*args)
43-
if args.length == 1 && Native === args[0]
43+
if args.length == 1 && native?(args[0])
4444
object = args[0]
4545

4646
members.each {|name|

spec/opal/native/each_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe "Native::Object#each" do
1+
describe "Native#each" do
22
it "enumerates on object properties" do
33
Native(`{ a: 2, b: 3 }`).each {|name, value|
44
((name == :a && value == 2) || (name == :b && value == 3)).should be_true
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe "Native::Object#[]" do
1+
describe "Native#[]" do
22
it "should return the same value for bridged classes" do
33
Native(`2`).should === 2
44
Native(`"lol"`).should === "lol"
@@ -8,7 +8,7 @@
88
Native(`{ a: function(){} }`)[:a].should be_kind_of Proc
99
end
1010

11-
it "should wrap natives into Native::Object" do
12-
Native(`{ a: { b: 2 } }`)[:a][:b].should === 2
11+
it "should wrap natives into a Native object" do
12+
Native(`{ a: { b: 2 } }`)[:a][:b].should == 2
1313
end
1414
end

spec/opal/native/method_missing_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe "Native::Object#method_missing" do
1+
describe "Native#method_missing" do
22
it "should return values" do
33
Native(`{ a: 23 }`).a.should == 23
44
Native(`{ a: { b: 42 } }`).a.b.should == 42

spec/opal/native/nil_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe "Native::Object#nil?" do
1+
describe "Native#nil?" do
22
it "returns true for wrapped null" do
33
Native(`null`).nil?.should be_true
44
end

stdlib/buffer.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
require 'buffer/array'
22
require 'buffer/view'
33

4-
class Buffer < Native
4+
class Buffer
5+
include Native::Base
6+
57
def self.supported?
6-
`typeof(ArrayBuffer) != "undefined"`
8+
not $$[:ArrayBuffer].nil?
79
end
810

911
def self.name_for(bits, type)
@@ -15,7 +17,7 @@ def self.name_for(bits, type)
1517
end
1618

1719
def initialize(size, bits = 8)
18-
if Native === size
20+
if native?(size)
1921
super(size)
2022
else
2123
super(`new ArrayBuffer(size * (bits / 8))`)

stdlib/buffer/view.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
class Buffer < Native
1+
class Buffer
2+
3+
class View
4+
include Native::Base
25

3-
class View < Native
46
def self.supported?
5-
`typeof(DataView) != "undefined"`
7+
not $$[:DataView].nil?
68
end
79

810
attr_reader :buffer, :offset
911

1012
def initialize(buffer, offset = nil, length = nil)
11-
if Native === buffer
13+
if native?(buffer)
1214
super(buffer)
1315
elsif offset && length
1416
super(`new DataView(#{buffer.to_n}, #{offset.to_n}, #{length.to_n})`)

0 commit comments

Comments
 (0)