opal-native 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/.gitignore +1 -0
  2. data/Rakefile +14 -0
  3. data/lib/opal/native.rb +234 -0
  4. data/opal-native.gemspec +16 -0
  5. metadata +59 -0
@@ -0,0 +1 @@
1
+ /build
@@ -0,0 +1,14 @@
1
+ require 'bundler/setup'
2
+ require 'opal'
3
+
4
+ desc 'Build specified dependencies into .'
5
+ task :dependencies do
6
+ Opal::DependencyBuilder.new(stdlib: 'native', out: 'build').build
7
+ end
8
+
9
+ desc 'Build latest opal-native to current dir'
10
+ task :build do
11
+ Opal::Builder.new('lib', join: 'build/opal-native.js').build
12
+ end
13
+
14
+ task :default => :build
@@ -0,0 +1,234 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+
11
+ module Native
12
+ class Object
13
+ include Native
14
+
15
+ def [](name)
16
+ `#@native[name]`
17
+ end
18
+
19
+ def []=(name, value)
20
+ `#@native[name] = value`
21
+ end
22
+
23
+ def nil?
24
+ `#@native === null || #@native === undefined`
25
+ end
26
+
27
+ def method_missing(name, *args)
28
+ return super unless Opal.function? `#@native[name]`
29
+
30
+ __native_send__ name, *args
31
+ end
32
+ end
33
+
34
+ def self.included (klass)
35
+ class << klass
36
+ def from_native (object)
37
+ instance = allocate
38
+ instance.instance_variable_set :@native, object
39
+
40
+ instance
41
+ end
42
+ end
43
+ end
44
+
45
+ def initialize(native)
46
+ @native = native
47
+ end
48
+
49
+ def to_native
50
+ @native
51
+ end
52
+
53
+ def native_send(name, *args)
54
+ return method_missing(name, *args) unless Opal.function? `#@native[name]`
55
+
56
+ `#@native[name].apply(#@native, args)`
57
+ end
58
+
59
+ alias_method :__native_send__, :native_send
60
+ end
61
+
62
+ class Module
63
+ %x{
64
+ function define_attr_bridge(klass, target, name, getter, setter) {
65
+ if (getter) {
66
+ define_method(klass, mid_to_jsid(name), function() {
67
+ var real_target = target;
68
+
69
+ if (target.$f & T_STRING) {
70
+ real_target = target[0] == '@' ? this[target.substr(1)] : this[mid_to_jsid(target)].apply(this, null);
71
+ }
72
+
73
+ var result = real_target[name];
74
+
75
+ return result == null ? nil : result;
76
+ });
77
+ }
78
+
79
+ if (setter) {
80
+ define_method(klass, mid_to_jsid(name + '='), function (block, val) {
81
+ var real_target = target;
82
+
83
+ if (target.$f & T_STRING) {
84
+ real_target = target[0] == '@' ? this[target.substr(1)] : this[mid_to_jsid(target)].apply(this, null);
85
+ }
86
+
87
+ return real_target[name] = val;
88
+ });
89
+ }
90
+ }
91
+ }
92
+
93
+ def attr_accessor_bridge(target, *attrs)
94
+ %x{
95
+ for (var i = 0, length = attrs.length; i < length; i++) {
96
+ define_attr_bridge(this, target, attrs[i], true, true);
97
+ }
98
+
99
+ return nil;
100
+ }
101
+ end
102
+
103
+ def attr_reader_bridge(target, *attrs)
104
+ %x{
105
+ for (var i = 0, length = attrs.length; i < length; i++) {
106
+ define_attr_bridge(this, target, attrs[i], true, false);
107
+ }
108
+
109
+ return nil;
110
+ }
111
+ end
112
+
113
+ def attr_reader_bridge(target, *attrs)
114
+ %x{
115
+ for (var i = 0, length = attrs.length; i < length; i++) {
116
+ define_attr_bridge(this, target, attrs[i], false, true);
117
+ }
118
+
119
+ return nil;
120
+ }
121
+ end
122
+
123
+ def attr_bridge(target, name, setter = false)
124
+ `define_attr_bridge(this, target, name, true, setter)`
125
+
126
+ self
127
+ end
128
+
129
+ %x{
130
+ function define_method_bridge(klass, target, id, name) {
131
+ define_method(klass, id, function() {
132
+ var real_target = target;
133
+
134
+ if (target.$f & T_STRING) {
135
+ real_target = target[0] == '@' ? this[target.substr(1)] : this[mid_to_jsid(target)].apply(this, null);
136
+ }
137
+
138
+ return real_target.apply(this, $slice.call(arguments, 1));
139
+ });
140
+ }
141
+ }
142
+
143
+ def define_method_bridge(object, name, ali = nil)
144
+ %x{
145
+ define_method_bridge(this, object, mid_to_jsid(#{ali || name}), name);
146
+ this.$methods.push(name);
147
+
148
+ return nil;
149
+ }
150
+ end
151
+ end
152
+
153
+ class Array
154
+ def to_native
155
+ map { |obj| Opal.object?(obj) ? obj.to_native : obj }
156
+ end
157
+ end
158
+
159
+ class Boolean
160
+ def to_native
161
+ `this == true`
162
+ end
163
+ end
164
+
165
+ class Hash
166
+ def to_native
167
+ %x{
168
+ var map = this.map,
169
+ result = {};
170
+
171
+ for (var assoc in map) {
172
+ var key = map[assoc][0],
173
+ value = map[assoc][1];
174
+
175
+ result[key] = #{Opal.native?(`value`)} ? value : #{`value`.to_native};
176
+ }
177
+
178
+ return result;
179
+ }
180
+ end
181
+ end
182
+
183
+ module Kernel
184
+ def Object(object)
185
+ Opal.native?(object) ? Native::Object.new(object) : object
186
+ end
187
+ end
188
+
189
+ class MatchData
190
+ alias to_native to_a
191
+ end
192
+
193
+ class NilClass
194
+ def to_native
195
+ `var result; return result;`
196
+ end
197
+ end
198
+
199
+ class Numeric
200
+ def to_native
201
+ `this.valueOf()`
202
+ end
203
+ end
204
+
205
+ class Object
206
+ def to_native
207
+ raise TypeError, 'no specialized #to_native has been implemented'
208
+ end
209
+ end
210
+
211
+ class Proc
212
+ def to_native
213
+ %x{
214
+ return function() {
215
+ var args = Array.slice.call(arguments);
216
+ args.unshift(null); // block
217
+
218
+ return this.apply(this.$S, args);
219
+ };
220
+ }
221
+ end
222
+ end
223
+
224
+ class Regexp
225
+ def to_native
226
+ self
227
+ end
228
+ end
229
+
230
+ class String
231
+ def to_native
232
+ `this.valueOf()`
233
+ end
234
+ end
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new {|s|
2
+ s.name = 'opal-native'
3
+ s.version = '0.0.1'
4
+ s.author = 'meh.'
5
+ s.email = 'meh@paranoici.org'
6
+ s.homepage = 'http://github.com/opal/opal-native'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.summary = 'Easy support for native objects in Opal'
9
+
10
+ s.files = `git ls-files`.split("\n")
11
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
12
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ s.require_paths = ['lib']
14
+
15
+ s.add_dependency 'opal'
16
+ }
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opal-native
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - meh.
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: opal
16
+ requirement: &21726440 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *21726440
25
+ description:
26
+ email: meh@paranoici.org
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - .gitignore
32
+ - Rakefile
33
+ - lib/opal/native.rb
34
+ - opal-native.gemspec
35
+ homepage: http://github.com/opal/opal-native
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.10
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Easy support for native objects in Opal
59
+ test_files: []