opal 0.3.10 → 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/runtime/loader.js DELETED
@@ -1,330 +0,0 @@
1
-
2
- /**
3
- Valid file extensions opal can load/run
4
- */
5
- var load_extensions = {};
6
-
7
- load_extensions['.js'] = function(loader, path) {
8
- var source = loader.file_contents(path);
9
- return load_execute_file(loader, source, path);
10
- };
11
-
12
- load_extensions['.rb'] = function(loader, path) {
13
- var source = loader.ruby_file_contents(path);
14
- return load_execute_file(loader, source, path);
15
- };
16
-
17
- /**
18
- Require a file by its given lib path/id, or a full path.
19
-
20
- @param {String} id lib path/name
21
- @return {Boolean}
22
- */
23
- var rb_require = Op.require = function(lib) {
24
- var resolved = Op.loader.resolve_lib(lib);
25
- var cached = Op.cache[resolved];
26
-
27
- // If we have a cache for this require then it has already been
28
- // required. We return false to indicate this.
29
- if (cached) return false;
30
-
31
- Op.cache[resolved] = true;
32
-
33
- // try/catch wrap entire file load?
34
- load_file(Op.loader, resolved);
35
-
36
- return true;
37
- };
38
-
39
- /**
40
- Sets the primary 'gem', by name, so we know which cwd to use etc.
41
- This can be changed at anytime, but it is only really recomended
42
- before the application is run.
43
-
44
- Also, if a gem with the given name cannot be found, then an error
45
- will/should be thrown.
46
-
47
- @param {String} name The root gem name to use
48
- */
49
- Op.primary = function(name) {
50
- Fs.cwd = '/' + name;
51
- };
52
-
53
- /**
54
- Just go ahead and run the given block of code. The passed function
55
- should rake the usual runtime, self and file variables which it will
56
- be passed.
57
-
58
- @param {Function} body
59
- */
60
- Op.run = function(body) {
61
- var res = Qnil;
62
-
63
- if (typeof body != 'function') {
64
- rb_raise(rb_eException, "Expected body to be a function");
65
- }
66
-
67
- // try {
68
- res = body(Rt, rb_top_self, "(opal)");
69
- // }
70
- // catch (err) {
71
- // var stack;
72
-
73
- // if (err.$message) {
74
- // puts(err.$k.__classid__ + ': ' + err.$message);
75
- // }
76
- // else if (err.message) {
77
- // puts(err.$k.__classid__ + ': ' + err.message);
78
- // }
79
- // else {
80
- // puts('NativeError: ' + err.message);
81
- // console.log(err);
82
- // }
83
- // }
84
- return res;
85
- };
86
-
87
- /**
88
- Register a simple lib file. This file is simply just put into the lib
89
- "directory" so it is ready to load"
90
-
91
- @param {String} name The lib/gem name
92
- @param {String, Function} info
93
- */
94
- Op.lib = function(name, info) {
95
- // make sure name if useful
96
- if (typeof name !== 'string') {
97
- rb_raise(rb_eException, "Cannot register a lib without a proper name");
98
- }
99
-
100
- // make sure info is useful
101
- if (typeof info === 'string' || typeof info === 'function') {
102
- return load_register_lib(name, info);
103
- }
104
-
105
- // something went wrong..
106
- rb_raise(rb_eException, "Invalid lib data for: " + name);
107
- };
108
-
109
- /**
110
- External api for defining a package. This takes an object that defines
111
- all the package info and files.
112
-
113
- @param {Object} info Package info
114
- */
115
- Op.package = function(info) {
116
- if (typeof info === 'object') {
117
- load_register_package(info);
118
- }
119
- else {
120
- rb_raise(rb_eException, "Invalid package data");
121
- }
122
- };
123
-
124
- /**
125
- Actually register a predefined package. This is for the browser context
126
- where package can be serialized into JSON and defined before hand.
127
-
128
- @param {Object} info Serialized gemspec
129
- */
130
- function load_register_package(info) {
131
- var factories = Op.loader.factories,
132
- paths = Op.loader.paths,
133
- name = info.name;
134
-
135
- // register all lib files
136
- var libs = info.libs || {};
137
-
138
- // root dir for gem is '/gem_name'
139
- var root_dir = '/' + name;
140
-
141
- // for now assume './lib' as dir for all libs (should be dynamic..)
142
- var lib_dir = '/' + name + '/lib/';
143
-
144
- // add lib dir to paths
145
- paths.unshift(fs_expand_path(fs_join(root_dir, lib_dir)));
146
-
147
- for (var lib in libs) {
148
- if (hasOwnProperty.call(libs, lib)) {
149
- var file_path = lib_dir + lib;
150
- Op.loader.factories[file_path] = libs[lib];
151
- Op.loader.libs[lib] = file_path;
152
- }
153
- }
154
-
155
- // register other info? (version etc??)
156
- }
157
-
158
- /**
159
- Register a single lib/file in browser before its needed. These libs
160
- are added to top level dir '/lib_name.rb'
161
-
162
- @param {String} name Lib name
163
- @param {Function, String} factory
164
- */
165
- function load_register_lib(name, factory) {
166
- var path = '/lib/' + name;
167
- Op.loader.factories[path] = factory;
168
- Op.loader.libs[name] = path;
169
- }
170
-
171
- /**
172
- The loader is the core machinery used for loading and executing libs
173
- within opal. An instance of opal will have a `.loader` property which
174
- is an instance of this Loader class. A Loader is responsible for
175
- finding, opening and reading contents of libs on disk. Within the
176
- browser a loader may use XHR requests or cached libs defined by JSON
177
- to load required libs/gems.
178
-
179
- @constructor
180
- @param {opal} opal Opal instance to use
181
- */
182
- function Loader(opal) {
183
- this.opal = opal;
184
- this.paths = ['', '/lib'];
185
- this.factories = {};
186
- this.libs = {};
187
- return this;
188
- }
189
-
190
- // For minimizing
191
- var Lp = Loader.prototype;
192
-
193
- /**
194
- The paths property is an array of disk paths in which to search for
195
- required modules. In the browser this functionality isn't really used.
196
-
197
- This array is created within the constructor method for uniqueness
198
- between instances for correct sandboxing.
199
- */
200
- Lp.paths = null;
201
-
202
- /**
203
- factories of registered packages, paths => function/string. This is
204
- generic, but in reality only the browser uses this, and it is treated
205
- as the mini filesystem. Not just factories can go here, anything can!
206
- Images, text, json, whatever.
207
- */
208
- Lp.factories = {};
209
-
210
- /**
211
- Resolves the path to the lib, which can then be used to load. This
212
- will throw an error if the module cannot be found. If this method
213
- returns a successful path, then subsequent methods can assume that
214
- the path exists.
215
-
216
- @param {String} lib The lib name/path to look for
217
- @return {String}
218
- */
219
- Lp.resolve_lib = function(lib) {
220
- var resolved = this.find_lib(lib, this.paths);
221
-
222
- if (!resolved) {
223
- raise(eLoadError, "no such file to load -- " + lib);
224
- }
225
-
226
- return resolved;
227
- };
228
-
229
- Lp.find_lib = function(id) {
230
- var libs = this.libs;
231
- var id_with_ext = id + '.rb';
232
-
233
- // try to load a lib path first - i.e. something in our load path
234
- if (libs[id_with_ext]) {
235
- return libs[id_with_ext];
236
- }
237
-
238
- // go through full paths..
239
-
240
- // next, incase our require() has a ruby extension..
241
- if (id.lastIndexOf('.rb') == id.length - 3) {
242
- // id = id.substr(0, id.length - 3);
243
- if (libs[id]) {
244
- return libs[id];
245
- }
246
- // if not..
247
- // return null;
248
- }
249
-
250
- return null;
251
- };
252
-
253
- /**
254
- Valid factory format for use in require();
255
- */
256
- Lp.valid_extensions = ['.js', '.rb'];
257
-
258
- /**
259
- Get lib contents for js files
260
- */
261
- Lp.file_contents = function(path) {
262
- return this.factories[path];
263
- };
264
-
265
- Lp.ruby_file_contents = function(path) {
266
- return this.factories[path];
267
- };
268
-
269
- /**
270
- Actually run file with resolved name.
271
-
272
- @param {Loader} loader
273
- @param {String} path
274
- */
275
- function load_file(loader, path) {
276
- var ext = load_extensions[PATH_RE.exec(path)[3] || '.js'];
277
-
278
- if (!ext) {
279
- rb_raise(rb_eException, "load_run_file - Bad extension for resolved path");
280
- }
281
-
282
- ext(loader, path);
283
- }
284
-
285
- /**
286
- Run content which must now be javascript. Arguments we pass to func
287
- are:
288
-
289
- $rb
290
- top_self
291
- filename
292
-
293
- @param {String, Function} content
294
- @param {String} path
295
- */
296
- function load_execute_file(loader, content, path) {
297
- var args = [Rt, rb_top_self, path];
298
-
299
- if (typeof content === 'function') {
300
- return content.apply(Op, args);
301
-
302
- } else if (typeof content === 'string') {
303
- var func = loader.wrap(content, path);
304
- return func.apply(Op, args);
305
-
306
- } else {
307
- rb_raise(rb_eException, "Loader.execute - bad content for: " + path);
308
- }
309
- }
310
-
311
- /**
312
- Getter method for getting the load path for opal.
313
-
314
- @param {String} id The globals id being retrieved.
315
- @return {Array} Load paths
316
- */
317
- function rb_load_path_getter(id) {
318
- return Op.loader.paths;
319
- }
320
-
321
- /**
322
- Getter method to get all loaded features.
323
-
324
- @param {String} id Feature global id
325
- @return {Array} Loaded features
326
- */
327
- function rb_loaded_feature_getter(id) {
328
- return loaded_features;
329
- }
330
-
data/runtime/module.js DELETED
@@ -1,103 +0,0 @@
1
-
2
- /**
3
- Define a top level module with the given id
4
- */
5
- function rb_define_module(id) {
6
- return rb_define_module_under(rb_cObject, id);
7
- };
8
-
9
- function rb_define_module_under(base, id) {
10
- var module;
11
-
12
- if (rb_const_defined(base, id)) {
13
- module = rb_const_get(base, id);
14
- if (module.$f & T_MODULE) {
15
- return module;
16
- }
17
-
18
- rb_raise(rb_eException, id + " is not a module");
19
- }
20
-
21
- module = rb_define_module_id(id);
22
-
23
- if (base == rb_cObject) {
24
- rb_name_class(module, id);
25
- } else {
26
- rb_name_class(module, base.__classid__ + '::' + id);
27
- }
28
-
29
- rb_const_set(base, id, module);
30
- module.$parent = base;
31
- return module;
32
- };
33
-
34
- function rb_define_module_id(id) {
35
- var module = rb_class_create(rb_cModule);
36
- rb_make_metaclass(module, rb_cModule);
37
-
38
- module.$f = T_MODULE;
39
- module.$included_in = [];
40
- return module;
41
- };
42
-
43
- function rb_mod_create() {
44
- return rb_class_boot(rb_cModule);
45
- };
46
-
47
- function rb_include_module(klass, module) {
48
-
49
- if (!klass.$included_modules) {
50
- klass.$included_modules = [];
51
- }
52
-
53
- if (klass.$included_modules.indexOf(module) != -1) {
54
- return;
55
- }
56
- klass.$included_modules.push(module);
57
-
58
- if (!module.$included_in) {
59
- module.$included_in = [];
60
- }
61
-
62
- module.$included_in.push(klass);
63
-
64
- for (var method in module.$m) {
65
- if (hasOwnProperty.call(module.$m, method)) {
66
- rb_define_raw_method(klass, method,
67
- module.$a.prototype[method]);
68
- }
69
- }
70
-
71
- // for (var constant in module.$c) {
72
- // if (hasOwnProperty.call(module.$c, constant)) {
73
- // const_set(klass, constant, module.$c[constant]);
74
- // }
75
- // }
76
- };
77
-
78
- function rb_extend_module(klass, module) {
79
- if (!klass.$extended_modules) {
80
- klass.$extended_modules = [];
81
- }
82
-
83
- if (klass.$extended_modules.indexOf(module) != -1) {
84
- return;
85
- }
86
- klass.$extended_modules.push(module);
87
-
88
- if (!module.$extended_in) {
89
- module.$extended_in = [];
90
- }
91
-
92
- module.$extended_in.push(klass);
93
-
94
- var meta = klass.$k;
95
-
96
- for (var method in module.o$m) {
97
- if (hasOwnProperty.call(module.o$m, method)) {
98
- rb_define_raw_method(meta, method,
99
- module.o$a.prototype[method]);
100
- }
101
- }
102
- };
103
-
data/runtime/post.js DELETED
@@ -1,10 +0,0 @@
1
-
2
- init();
3
-
4
- })(undefined);
5
-
6
- // if in a commonjs system already (node etc), exports become our opal
7
- // object. Otherwise, in the browser, we just get a top level opal var
8
- if ((typeof require !== 'undefined') && (typeof module !== 'undefined')) {
9
- module.exports = opalscript;
10
- }
data/runtime/pre.js DELETED
@@ -1,7 +0,0 @@
1
- opal = {};
2
-
3
- (function(undefined) {
4
-
5
- // So we can minimize
6
- var Op = opal;
7
-