modulr 0.1.0
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/LICENSE +22 -0
- data/README.markdown +62 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/assets/modulr.js +58 -0
- data/bin/modulrize +39 -0
- data/example/foo/bar.js +1 -0
- data/example/foo/foo.js +3 -0
- data/example/increment.js +6 -0
- data/example/inspect.js +3 -0
- data/example/math.js +7 -0
- data/example/program.js +9 -0
- data/lib/modulr/collector.rb +60 -0
- data/lib/modulr/js_module.rb +88 -0
- data/lib/modulr/version.rb +6 -0
- data/lib/modulr.rb +21 -0
- data/vendor/rkelly/CHANGELOG.rdoc +13 -0
- data/vendor/rkelly/Manifest.txt +198 -0
- data/vendor/rkelly/README.rdoc +58 -0
- data/vendor/rkelly/Rakefile +39 -0
- data/vendor/rkelly/lib/parser.y +870 -0
- data/vendor/rkelly/lib/rkelly/constants.rb +3 -0
- data/vendor/rkelly/lib/rkelly/generated_parser.rb +3237 -0
- data/vendor/rkelly/lib/rkelly/js/array.rb +15 -0
- data/vendor/rkelly/lib/rkelly/js/base.rb +91 -0
- data/vendor/rkelly/lib/rkelly/js/boolean.rb +21 -0
- data/vendor/rkelly/lib/rkelly/js/function.rb +39 -0
- data/vendor/rkelly/lib/rkelly/js/function_prototype.rb +15 -0
- data/vendor/rkelly/lib/rkelly/js/global_object.rb +52 -0
- data/vendor/rkelly/lib/rkelly/js/math.rb +10 -0
- data/vendor/rkelly/lib/rkelly/js/nan.rb +18 -0
- data/vendor/rkelly/lib/rkelly/js/number.rb +22 -0
- data/vendor/rkelly/lib/rkelly/js/object.rb +30 -0
- data/vendor/rkelly/lib/rkelly/js/object_prototype.rb +14 -0
- data/vendor/rkelly/lib/rkelly/js/property.rb +20 -0
- data/vendor/rkelly/lib/rkelly/js/scope.rb +6 -0
- data/vendor/rkelly/lib/rkelly/js/string.rb +21 -0
- data/vendor/rkelly/lib/rkelly/js.rb +14 -0
- data/vendor/rkelly/lib/rkelly/lexeme.rb +18 -0
- data/vendor/rkelly/lib/rkelly/nodes/binary_node.rb +18 -0
- data/vendor/rkelly/lib/rkelly/nodes/bracket_accessor_node.rb +11 -0
- data/vendor/rkelly/lib/rkelly/nodes/case_clause_node.rb +11 -0
- data/vendor/rkelly/lib/rkelly/nodes/comma_node.rb +11 -0
- data/vendor/rkelly/lib/rkelly/nodes/conditional_node.rb +11 -0
- data/vendor/rkelly/lib/rkelly/nodes/dot_accessor_node.rb +11 -0
- data/vendor/rkelly/lib/rkelly/nodes/for_in_node.rb +12 -0
- data/vendor/rkelly/lib/rkelly/nodes/for_node.rb +13 -0
- data/vendor/rkelly/lib/rkelly/nodes/function_call_node.rb +16 -0
- data/vendor/rkelly/lib/rkelly/nodes/function_decl_node.rb +6 -0
- data/vendor/rkelly/lib/rkelly/nodes/function_expr_node.rb +12 -0
- data/vendor/rkelly/lib/rkelly/nodes/if_node.rb +12 -0
- data/vendor/rkelly/lib/rkelly/nodes/label_node.rb +11 -0
- data/vendor/rkelly/lib/rkelly/nodes/new_expr_node.rb +11 -0
- data/vendor/rkelly/lib/rkelly/nodes/node.rb +88 -0
- data/vendor/rkelly/lib/rkelly/nodes/not_strict_equal_node.rb +6 -0
- data/vendor/rkelly/lib/rkelly/nodes/op_equal_node.rb +16 -0
- data/vendor/rkelly/lib/rkelly/nodes/postfix_node.rb +11 -0
- data/vendor/rkelly/lib/rkelly/nodes/prefix_node.rb +6 -0
- data/vendor/rkelly/lib/rkelly/nodes/property_node.rb +13 -0
- data/vendor/rkelly/lib/rkelly/nodes/resolve_node.rb +19 -0
- data/vendor/rkelly/lib/rkelly/nodes/strict_equal_node.rb +6 -0
- data/vendor/rkelly/lib/rkelly/nodes/try_node.rb +13 -0
- data/vendor/rkelly/lib/rkelly/nodes/var_decl_node.rb +15 -0
- data/vendor/rkelly/lib/rkelly/nodes.rb +25 -0
- data/vendor/rkelly/lib/rkelly/parser.rb +104 -0
- data/vendor/rkelly/lib/rkelly/runtime/ruby_function.rb +13 -0
- data/vendor/rkelly/lib/rkelly/runtime/scope_chain.rb +57 -0
- data/vendor/rkelly/lib/rkelly/runtime.rb +36 -0
- data/vendor/rkelly/lib/rkelly/syntax_error.rb +4 -0
- data/vendor/rkelly/lib/rkelly/token.rb +15 -0
- data/vendor/rkelly/lib/rkelly/tokenizer.rb +122 -0
- data/vendor/rkelly/lib/rkelly/visitable.rb +16 -0
- data/vendor/rkelly/lib/rkelly/visitors/dot_visitor.rb +228 -0
- data/vendor/rkelly/lib/rkelly/visitors/ecma_visitor.rb +314 -0
- data/vendor/rkelly/lib/rkelly/visitors/enumerable_visitor.rb +18 -0
- data/vendor/rkelly/lib/rkelly/visitors/evaluation_visitor.rb +419 -0
- data/vendor/rkelly/lib/rkelly/visitors/function_visitor.rb +46 -0
- data/vendor/rkelly/lib/rkelly/visitors/pointcut_visitor.rb +31 -0
- data/vendor/rkelly/lib/rkelly/visitors/real_sexp_visitor.rb +16 -0
- data/vendor/rkelly/lib/rkelly/visitors/sexp_visitor.rb +373 -0
- data/vendor/rkelly/lib/rkelly/visitors/visitor.rb +136 -0
- data/vendor/rkelly/lib/rkelly/visitors.rb +4 -0
- data/vendor/rkelly/lib/rkelly.rb +14 -0
- data/vendor/rkelly/rkelly.gemspec +34 -0
- data/vendor/rkelly/test/ecma_script_test_case.rb +21 -0
- data/vendor/rkelly/test/execute_test_case.rb +16 -0
- data/vendor/rkelly/test/execution_contexts/test_10_1_3-1.rb +32 -0
- data/vendor/rkelly/test/expressions/test_11_3_1.rb +64 -0
- data/vendor/rkelly/test/expressions/test_11_3_2.rb +64 -0
- data/vendor/rkelly/test/expressions/test_11_4_2.rb +13 -0
- data/vendor/rkelly/test/expressions/test_11_4_3.rb +52 -0
- data/vendor/rkelly/test/expressions/test_11_4_4.rb +68 -0
- data/vendor/rkelly/test/expressions/test_11_4_5.rb +69 -0
- data/vendor/rkelly/test/expressions/test_11_4_6.rb +88 -0
- data/vendor/rkelly/test/expressions/test_11_4_8.rb +28 -0
- data/vendor/rkelly/test/expressions/test_11_4_9.rb +103 -0
- data/vendor/rkelly/test/expressions/test_11_5_1.rb +51 -0
- data/vendor/rkelly/test/expressions/test_11_5_2.rb +80 -0
- data/vendor/rkelly/test/expressions/test_11_5_3.rb +88 -0
- data/vendor/rkelly/test/expressions/test_11_6_1-1.rb +19 -0
- data/vendor/rkelly/test/expressions/test_11_9_1.rb +19 -0
- data/vendor/rkelly/test/function/test_15_3_1_1-1.rb +34 -0
- data/vendor/rkelly/test/global_object/test_15_1_1_1.rb +29 -0
- data/vendor/rkelly/test/global_object/test_15_1_1_2.rb +17 -0
- data/vendor/rkelly/test/global_object/test_15_1_1_3.rb +9 -0
- data/vendor/rkelly/test/helper.rb +5 -0
- data/vendor/rkelly/test/node_test_case.rb +11 -0
- data/vendor/rkelly/test/object/test_15_2_1_1.rb +257 -0
- data/vendor/rkelly/test/object/test_15_2_1_2.rb +21 -0
- data/vendor/rkelly/test/object/test_15_2_2_1.rb +52 -0
- data/vendor/rkelly/test/statements/test_12_5-1.rb +27 -0
- data/vendor/rkelly/test/test_add_node.rb +8 -0
- data/vendor/rkelly/test/test_arguments_node.rb +8 -0
- data/vendor/rkelly/test/test_array_node.rb +9 -0
- data/vendor/rkelly/test/test_assign_expr_node.rb +8 -0
- data/vendor/rkelly/test/test_automatic_semicolon_insertion.rb +137 -0
- data/vendor/rkelly/test/test_bit_and_node.rb +8 -0
- data/vendor/rkelly/test/test_bit_or_node.rb +8 -0
- data/vendor/rkelly/test/test_bit_x_or_node.rb +8 -0
- data/vendor/rkelly/test/test_bitwise_not_node.rb +8 -0
- data/vendor/rkelly/test/test_block_node.rb +14 -0
- data/vendor/rkelly/test/test_bracket_accessor_node.rb +16 -0
- data/vendor/rkelly/test/test_break_node.rb +11 -0
- data/vendor/rkelly/test/test_case_block_node.rb +11 -0
- data/vendor/rkelly/test/test_case_clause_node.rb +15 -0
- data/vendor/rkelly/test/test_comma_node.rb +13 -0
- data/vendor/rkelly/test/test_comments.rb +44 -0
- data/vendor/rkelly/test/test_conditional_node.rb +17 -0
- data/vendor/rkelly/test/test_const_statement_node.rb +14 -0
- data/vendor/rkelly/test/test_continue_node.rb +11 -0
- data/vendor/rkelly/test/test_delete_node.rb +8 -0
- data/vendor/rkelly/test/test_divide_node.rb +8 -0
- data/vendor/rkelly/test/test_do_while_node.rb +13 -0
- data/vendor/rkelly/test/test_dot_accessor_node.rb +9 -0
- data/vendor/rkelly/test/test_ecma_visitor.rb +192 -0
- data/vendor/rkelly/test/test_element_node.rb +8 -0
- data/vendor/rkelly/test/test_empty_statement_node.rb +8 -0
- data/vendor/rkelly/test/test_equal_node.rb +8 -0
- data/vendor/rkelly/test/test_evaluation_visitor.rb +66 -0
- data/vendor/rkelly/test/test_expression_statement_node.rb +10 -0
- data/vendor/rkelly/test/test_false_node.rb +8 -0
- data/vendor/rkelly/test/test_for_in_node.rb +17 -0
- data/vendor/rkelly/test/test_for_node.rb +24 -0
- data/vendor/rkelly/test/test_function_body_node.rb +8 -0
- data/vendor/rkelly/test/test_function_call_node.rb +10 -0
- data/vendor/rkelly/test/test_function_decl_node.rb +16 -0
- data/vendor/rkelly/test/test_function_expr_node.rb +16 -0
- data/vendor/rkelly/test/test_function_visitor.rb +26 -0
- data/vendor/rkelly/test/test_getter_property_node.rb +10 -0
- data/vendor/rkelly/test/test_global_object.rb +49 -0
- data/vendor/rkelly/test/test_greater_node.rb +8 -0
- data/vendor/rkelly/test/test_greater_or_equal_node.rb +8 -0
- data/vendor/rkelly/test/test_if_node.rb +17 -0
- data/vendor/rkelly/test/test_in_node.rb +8 -0
- data/vendor/rkelly/test/test_instance_of_node.rb +8 -0
- data/vendor/rkelly/test/test_label_node.rb +13 -0
- data/vendor/rkelly/test/test_left_shift_node.rb +8 -0
- data/vendor/rkelly/test/test_less_node.rb +8 -0
- data/vendor/rkelly/test/test_less_or_equal_node.rb +8 -0
- data/vendor/rkelly/test/test_line_number.rb +23 -0
- data/vendor/rkelly/test/test_logical_and_node.rb +8 -0
- data/vendor/rkelly/test/test_logical_not_node.rb +8 -0
- data/vendor/rkelly/test/test_logical_or_node.rb +8 -0
- data/vendor/rkelly/test/test_modulus_node.rb +8 -0
- data/vendor/rkelly/test/test_multiply_node.rb +8 -0
- data/vendor/rkelly/test/test_new_expr_node.rb +9 -0
- data/vendor/rkelly/test/test_not_equal_node.rb +8 -0
- data/vendor/rkelly/test/test_not_strict_equal_node.rb +8 -0
- data/vendor/rkelly/test/test_null_node.rb +8 -0
- data/vendor/rkelly/test/test_number_node.rb +8 -0
- data/vendor/rkelly/test/test_object_literal_node.rb +9 -0
- data/vendor/rkelly/test/test_op_and_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_op_divide_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_op_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_op_l_shift_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_op_minus_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_op_mod_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_op_multiply_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_op_or_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_op_plus_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_op_r_shift_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_op_u_r_shift_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_op_x_or_equal_node.rb +10 -0
- data/vendor/rkelly/test/test_parameter_node.rb +8 -0
- data/vendor/rkelly/test/test_parser.rb +1361 -0
- data/vendor/rkelly/test/test_pointcut_visitor.rb +34 -0
- data/vendor/rkelly/test/test_postfix_node.rb +8 -0
- data/vendor/rkelly/test/test_prefix_node.rb +8 -0
- data/vendor/rkelly/test/test_property_node.rb +8 -0
- data/vendor/rkelly/test/test_regexp_node.rb +8 -0
- data/vendor/rkelly/test/test_resolve_node.rb +22 -0
- data/vendor/rkelly/test/test_return_node.rb +11 -0
- data/vendor/rkelly/test/test_right_shift_node.rb +8 -0
- data/vendor/rkelly/test/test_rkelly.rb +19 -0
- data/vendor/rkelly/test/test_runtime.rb +12 -0
- data/vendor/rkelly/test/test_scope_chain.rb +50 -0
- data/vendor/rkelly/test/test_setter_property_node.rb +10 -0
- data/vendor/rkelly/test/test_source_elements.rb +9 -0
- data/vendor/rkelly/test/test_strict_equal_node.rb +8 -0
- data/vendor/rkelly/test/test_string_node.rb +8 -0
- data/vendor/rkelly/test/test_subtract_node.rb +8 -0
- data/vendor/rkelly/test/test_switch_node.rb +12 -0
- data/vendor/rkelly/test/test_this_node.rb +8 -0
- data/vendor/rkelly/test/test_throw_node.rb +7 -0
- data/vendor/rkelly/test/test_tokenizer.rb +148 -0
- data/vendor/rkelly/test/test_true_node.rb +8 -0
- data/vendor/rkelly/test/test_try_node.rb +59 -0
- data/vendor/rkelly/test/test_type_of_node.rb +8 -0
- data/vendor/rkelly/test/test_unary_minus_node.rb +8 -0
- data/vendor/rkelly/test/test_unary_plus_node.rb +8 -0
- data/vendor/rkelly/test/test_unsigned_right_shift_node.rb +8 -0
- data/vendor/rkelly/test/test_var_decl_node.rb +21 -0
- data/vendor/rkelly/test/test_var_statement_node.rb +14 -0
- data/vendor/rkelly/test/test_void_node.rb +8 -0
- data/vendor/rkelly/test/test_while_node.rb +15 -0
- data/vendor/rkelly/test/test_with_node.rb +8 -0
- metadata +271 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2010 codespeaks sàrl
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
modulr
|
2
|
+
======
|
3
|
+
|
4
|
+
Description
|
5
|
+
-----------
|
6
|
+
|
7
|
+
`modulr` is a [CommonJS module implementation](http://commonjs.org/specs/modules/1.0.html)
|
8
|
+
in Ruby for client-side JavaScript.
|
9
|
+
|
10
|
+
* [Github repository](http://github.com/codespeaks/modulr)
|
11
|
+
* [Specification](http://wiki.commonjs.org/wiki/Modules/1.0)
|
12
|
+
|
13
|
+
Install
|
14
|
+
-------
|
15
|
+
|
16
|
+
$ [sudo] gem install modulr
|
17
|
+
|
18
|
+
Usage
|
19
|
+
-----
|
20
|
+
|
21
|
+
`modulr` accepts a singular file as input (the _program_) on which is does static
|
22
|
+
analysis to recursively resolve its dependencies.
|
23
|
+
|
24
|
+
The program, its dependencies and a small, namespaced JavaScript library are concatenated into a single `js` file. This
|
25
|
+
[improves load times by minimizing HTTP requests](http://developer.yahoo.com/performance/rules.html#num_http).
|
26
|
+
|
27
|
+
The bundled JavaScript library provides each module with the necessary `require`
|
28
|
+
function and `exports` and `module` free variables.
|
29
|
+
|
30
|
+
`modulr` is available as a Ruby library or as a command-line utility (`modulrize`).
|
31
|
+
|
32
|
+
To process a JavaScript source file, just run:
|
33
|
+
|
34
|
+
$ modulrize filename.js > output.js
|
35
|
+
|
36
|
+
For a comprehensive list of options:
|
37
|
+
|
38
|
+
$ modulrize --help"
|
39
|
+
|
40
|
+
Specs
|
41
|
+
-----
|
42
|
+
|
43
|
+
To run the specs, first clone the Git repository then grab the CommonJS
|
44
|
+
specs, included as a Git submodule, by running:
|
45
|
+
|
46
|
+
$ git clone git://github.com/codespeaks/modulr.git
|
47
|
+
$ cd modulr
|
48
|
+
$ git submodule init
|
49
|
+
$ git submodule update
|
50
|
+
|
51
|
+
[Mozilla's SpiderMonkey](http://www.mozilla.org/js/spidermonkey/) is required
|
52
|
+
and the `js` command line executable must be available on the load path (try `which js`).
|
53
|
+
|
54
|
+
You can run all the specs by issuing:
|
55
|
+
|
56
|
+
$ rake spec
|
57
|
+
|
58
|
+
Alternatively, a list of comma-separated specs can be specified through the `SPECS`
|
59
|
+
environment variable (see `vendor/commonjs/tests/modules/1.0`) for a comprehensive
|
60
|
+
list of available specs).
|
61
|
+
|
62
|
+
$ rake spec SPECS=absolute,transitive
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'lib/modulr'
|
4
|
+
|
5
|
+
COMMONJS_SPEC_DIR = File.join('vendor', 'commonjs', 'tests', 'modules', '1.0')
|
6
|
+
|
7
|
+
desc "Concatenate example file"
|
8
|
+
task :build_example do
|
9
|
+
File.open(File.join('output', 'example.js'), 'w') do |f|
|
10
|
+
f << Modulr.ize(File.join('example', 'program.js'))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Run CommonJS Module 1.0 specs"
|
15
|
+
task :spec do
|
16
|
+
specs = ENV["SPECS"] || "**"
|
17
|
+
|
18
|
+
FileList["#{COMMONJS_SPEC_DIR}/{#{specs}}/program.js"].each do |spec|
|
19
|
+
dir = File.dirname(spec)
|
20
|
+
output = File.join(dir, 'output.js')
|
21
|
+
system = File.join(dir, 'system.js')
|
22
|
+
FileUtils.touch(system)
|
23
|
+
begin
|
24
|
+
puts File.basename(dir).center(80, "_")
|
25
|
+
File.open(output, 'w') do |f|
|
26
|
+
f << Modulr.ize(spec)
|
27
|
+
end
|
28
|
+
system("js -f #{output}")
|
29
|
+
rescue => e
|
30
|
+
phase = e.is_a?(Modulr::ModulrError) ? "building" : "running"
|
31
|
+
puts "ERROR while #{phase} (#{e.class}):"
|
32
|
+
puts e.message
|
33
|
+
ensure
|
34
|
+
FileUtils.rm(output)
|
35
|
+
FileUtils.rm(system)
|
36
|
+
puts
|
37
|
+
puts
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
begin
|
43
|
+
require "jeweler"
|
44
|
+
Jeweler::Tasks.new do |gemspec|
|
45
|
+
gemspec.name = "modulr"
|
46
|
+
gemspec.summary = "A CommonJS module implementation in Ruby for client-side JavaScript"
|
47
|
+
gemspec.author = "Tobie Langel"
|
48
|
+
gemspec.email = "tobie.langel@gmail.com"
|
49
|
+
gemspec.homepage = "http://github.com/tobie/modulr"
|
50
|
+
gemspec.files = FileList["Rakefile", "README.markdown", "LICENSE", "VERSION", "{lib,bin,assets,example}/**/*", "vendor/rkelly/**/*"]
|
51
|
+
gemspec.executable = "modulrize"
|
52
|
+
end
|
53
|
+
rescue LoadError
|
54
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
55
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/assets/modulr.js
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
// modulr (c) 2010 codespeaks sàrl
|
2
|
+
// Freely distributable under the terms of the MIT license.
|
3
|
+
// For details, see:
|
4
|
+
// http://github.com/codespeaks/modulr/blob/master/LICENSE
|
5
|
+
|
6
|
+
var modulr = (function(global) {
|
7
|
+
var _modules = {},
|
8
|
+
_moduleObjects = {},
|
9
|
+
_references = {},
|
10
|
+
_exports = {},
|
11
|
+
PREFIX = '__module__'; // Prefix identifiers to avoid issues in IE.
|
12
|
+
|
13
|
+
function log(str) {
|
14
|
+
if (global.console && console.log) { console.log(str); }
|
15
|
+
}
|
16
|
+
|
17
|
+
function require(identifier) {
|
18
|
+
var fn, modObj,
|
19
|
+
id = _references[PREFIX + identifier],
|
20
|
+
expts = _exports[id];
|
21
|
+
|
22
|
+
log('Required module "' + identifier + '".');
|
23
|
+
|
24
|
+
if (!expts) {
|
25
|
+
_exports[id] = expts = {};
|
26
|
+
_moduleObjects[id] = modObj = { id: id.replace(PREFIX, '') };
|
27
|
+
|
28
|
+
if (!require.main) { require.main = modObj; }
|
29
|
+
|
30
|
+
fn = _modules[id];
|
31
|
+
if (!fn) { throw 'Can\'t find module "' + identifier + '".'; }
|
32
|
+
fn(require, expts, modObj);
|
33
|
+
}
|
34
|
+
return expts;
|
35
|
+
}
|
36
|
+
|
37
|
+
function cache(identifier, id, fn) {
|
38
|
+
log('Cached module "' + identifier + '".');
|
39
|
+
id = PREFIX + id;
|
40
|
+
|
41
|
+
if (_modules[id]) {
|
42
|
+
throw 'Can\'t overwrite module "' + identifier + '".';
|
43
|
+
}
|
44
|
+
_modules[id] = fn;
|
45
|
+
_references[PREFIX + identifier] = id;
|
46
|
+
}
|
47
|
+
|
48
|
+
function alias(identifier, id) {
|
49
|
+
log('Linked "' + identifier + '" to module "' + id + '".');
|
50
|
+
_references[PREFIX + identifier] = PREFIX + id;
|
51
|
+
}
|
52
|
+
|
53
|
+
return {
|
54
|
+
require: require,
|
55
|
+
cache: cache,
|
56
|
+
alias: alias
|
57
|
+
};
|
58
|
+
})(this);
|
data/bin/modulrize
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.dirname(__FILE__) + '/../lib/modulr'
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
options = {
|
6
|
+
:output => STDOUT
|
7
|
+
}
|
8
|
+
|
9
|
+
opts = OptionParser.new do |opts|
|
10
|
+
opts.banner = 'Usage: modulrize [options] FILE'
|
11
|
+
|
12
|
+
opts.on('-o', '--output=FILE', 'Write the output to FILE. Defaults to stdout') do |output|
|
13
|
+
options[:output] = File.open(output, 'w')
|
14
|
+
end
|
15
|
+
|
16
|
+
opts.on('-r', '--root=DIR', 'Set DIR as root directory. Defaults to the directory containing FILE') do |root|
|
17
|
+
options[:root] = root
|
18
|
+
end
|
19
|
+
|
20
|
+
opts.on_tail('-h', '--help', 'Show this message') do
|
21
|
+
puts opts
|
22
|
+
exit
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
opts.parse!
|
27
|
+
|
28
|
+
output = options.delete(:output)
|
29
|
+
|
30
|
+
begin
|
31
|
+
if ARGV.size != 1
|
32
|
+
puts opts
|
33
|
+
exit 1
|
34
|
+
end
|
35
|
+
result = Modulr.ize(ARGV.first, options)
|
36
|
+
output.print(result)
|
37
|
+
ensure
|
38
|
+
output.close
|
39
|
+
end
|
data/example/foo/bar.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
exports.bar = 42;
|
data/example/foo/foo.js
ADDED
data/example/inspect.js
ADDED
data/example/math.js
ADDED
data/example/program.js
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
var inc = require('increment').increment;
|
2
|
+
var inspect = require('inspect').inspect;
|
3
|
+
inspect(inc(44));
|
4
|
+
inspect(require('inspect') === require('inspect'));
|
5
|
+
|
6
|
+
var foo = require('foo/foo');
|
7
|
+
inspect(foo.relative);
|
8
|
+
inspect(foo.relative === foo.relative2);
|
9
|
+
inspect(foo.toplevel === require('math').add);
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rkelly'
|
2
|
+
|
3
|
+
module Modulr
|
4
|
+
class Collector
|
5
|
+
attr_reader :modules, :aliases, :main
|
6
|
+
|
7
|
+
def initialize(root = nil)
|
8
|
+
@root = root
|
9
|
+
@modules = {}
|
10
|
+
@aliases = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse_file(path)
|
14
|
+
@src = File.read(path)
|
15
|
+
@root ||= File.dirname(path)
|
16
|
+
@main = JSModule.new(File.basename(path, '.js'), @root, path)
|
17
|
+
modules[main.id] = main
|
18
|
+
find_dependencies(@src, path)
|
19
|
+
end
|
20
|
+
|
21
|
+
def parser
|
22
|
+
@parser ||= RKelly::Parser.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def parse(src)
|
26
|
+
parser.parse(src)
|
27
|
+
end
|
28
|
+
|
29
|
+
def find_dependencies(src, path)
|
30
|
+
parse(src).each do |exp|
|
31
|
+
if is_a_require_expression?(exp)
|
32
|
+
js_module = JSModule.from_expression(exp, @root, path)
|
33
|
+
if cached_module = modules[js_module.id]
|
34
|
+
if cached_module.identifier != js_module.identifier
|
35
|
+
aliases[js_module.identifier] = js_module.id
|
36
|
+
end
|
37
|
+
else
|
38
|
+
modules[js_module.id] = js_module
|
39
|
+
find_dependencies(js_module.src, js_module.path)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def is_a_require_expression?(node)
|
46
|
+
node.is_a?(RKelly::Nodes::FunctionCallNode) &&
|
47
|
+
node.value.is_a?(RKelly::Nodes::ResolveNode) &&
|
48
|
+
node.value.value == 'require'
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_js(buffer = '')
|
52
|
+
buffer << File.read(PATH_TO_MODULR_JS);
|
53
|
+
modules.each { |id, js_module| js_module.to_js(buffer) }
|
54
|
+
aliases.each do |identifier, id|
|
55
|
+
buffer << "modulr.alias('#{identifier}', '#{id}');\n"
|
56
|
+
end
|
57
|
+
buffer << "\nmodulr.require('#{main.identifier}');\n"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module Modulr
|
2
|
+
class JSModule
|
3
|
+
def self.from_expression(exp, root, file)
|
4
|
+
str = exp.arguments.first.value.first
|
5
|
+
new(str.value[1...-1], root, file, str.line.to_i)
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :identifier, :root, :terms, :file, :line
|
9
|
+
|
10
|
+
def initialize(identifier, root, file=nil, line=nil)
|
11
|
+
@identifier = identifier
|
12
|
+
@root = root
|
13
|
+
@file = file
|
14
|
+
@line = line
|
15
|
+
@terms = identifier.split('/').reject { |term| term == '' }
|
16
|
+
raise ModuleIdentifierError.new(self) unless identifier_valid?
|
17
|
+
end
|
18
|
+
|
19
|
+
def inspect
|
20
|
+
"#<#{self.class.name} \"#{identifier}\">"
|
21
|
+
end
|
22
|
+
|
23
|
+
def identifier_valid?
|
24
|
+
@valid ||= terms.all? { |t| t =~ /^([a-zA-Z]+|\.\.?)$/ }
|
25
|
+
end
|
26
|
+
|
27
|
+
def id
|
28
|
+
return @id if @id
|
29
|
+
@id = File.expand_path(partial_path, directory)
|
30
|
+
@id.sub!("#{File.expand_path(root)}/", '')
|
31
|
+
end
|
32
|
+
|
33
|
+
def relative?
|
34
|
+
@relative ||= terms.first =~ /^\.\.?$/
|
35
|
+
end
|
36
|
+
|
37
|
+
def top_level?
|
38
|
+
!relative?
|
39
|
+
end
|
40
|
+
|
41
|
+
def path
|
42
|
+
@path ||= File.expand_path(partial_path, directory) + '.js'
|
43
|
+
end
|
44
|
+
|
45
|
+
def src
|
46
|
+
return @src if @src
|
47
|
+
if File.exist?(path)
|
48
|
+
@src = File.read(path)
|
49
|
+
else
|
50
|
+
raise LoadModuleError.new(self)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_js(buffer = '')
|
55
|
+
fn = "function(require, exports, module) {\n#{src}\n}"
|
56
|
+
buffer << "modulr.cache('#{identifier}', '#{id}', #{fn});"
|
57
|
+
end
|
58
|
+
|
59
|
+
protected
|
60
|
+
def partial_path
|
61
|
+
File.join(*terms)
|
62
|
+
end
|
63
|
+
|
64
|
+
def directory
|
65
|
+
if relative?
|
66
|
+
File.dirname(file)
|
67
|
+
else
|
68
|
+
root
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class ModuleIdentifierError < ModulrError
|
74
|
+
attr_reader :js_module
|
75
|
+
def initialize(js_module)
|
76
|
+
@js_module = js_module
|
77
|
+
super("Invalid module identifier '#{js_module.identifier}' in #{js_module.file} at line #{js_module.line}.")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class LoadModuleError < ModulrError
|
82
|
+
attr_reader :js_module
|
83
|
+
def initialize(js_module)
|
84
|
+
@js_module = js_module
|
85
|
+
super("Cannot load module '#{js_module.identifier}' in #{js_module.file} at line #{js_module.line}.\nMissing file #{js_module.path}.")
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/modulr.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Modulr
|
2
|
+
LIB_DIR = File.dirname(__FILE__)
|
3
|
+
PARSER_DIR = File.join(LIB_DIR, '..', 'vendor', 'rkelly', 'lib')
|
4
|
+
$:.unshift(LIB_DIR)
|
5
|
+
$:.unshift(PARSER_DIR)
|
6
|
+
|
7
|
+
class ModulrError < StandardError
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'modulr/js_module'
|
11
|
+
require 'modulr/collector'
|
12
|
+
require 'modulr/version'
|
13
|
+
|
14
|
+
PATH_TO_MODULR_JS = File.join(LIB_DIR, '..', 'assets', 'modulr.js')
|
15
|
+
|
16
|
+
def self.ize(input_filename, options = {})
|
17
|
+
collector = Collector.new(options[:root])
|
18
|
+
collector.parse_file(input_filename)
|
19
|
+
collector.to_js
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
CHANGELOG.rdoc
|
2
|
+
Manifest.txt
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
lib/parser.y
|
6
|
+
lib/rkelly.rb
|
7
|
+
lib/rkelly/constants.rb
|
8
|
+
lib/rkelly/generated_parser.rb
|
9
|
+
lib/rkelly/js.rb
|
10
|
+
lib/rkelly/js/array.rb
|
11
|
+
lib/rkelly/js/base.rb
|
12
|
+
lib/rkelly/js/boolean.rb
|
13
|
+
lib/rkelly/js/function.rb
|
14
|
+
lib/rkelly/js/function_prototype.rb
|
15
|
+
lib/rkelly/js/global_object.rb
|
16
|
+
lib/rkelly/js/math.rb
|
17
|
+
lib/rkelly/js/nan.rb
|
18
|
+
lib/rkelly/js/number.rb
|
19
|
+
lib/rkelly/js/object.rb
|
20
|
+
lib/rkelly/js/object_prototype.rb
|
21
|
+
lib/rkelly/js/property.rb
|
22
|
+
lib/rkelly/js/scope.rb
|
23
|
+
lib/rkelly/js/string.rb
|
24
|
+
lib/rkelly/lexeme.rb
|
25
|
+
lib/rkelly/nodes.rb
|
26
|
+
lib/rkelly/nodes/binary_node.rb
|
27
|
+
lib/rkelly/nodes/bracket_accessor_node.rb
|
28
|
+
lib/rkelly/nodes/case_clause_node.rb
|
29
|
+
lib/rkelly/nodes/comma_node.rb
|
30
|
+
lib/rkelly/nodes/conditional_node.rb
|
31
|
+
lib/rkelly/nodes/dot_accessor_node.rb
|
32
|
+
lib/rkelly/nodes/for_in_node.rb
|
33
|
+
lib/rkelly/nodes/for_node.rb
|
34
|
+
lib/rkelly/nodes/function_call_node.rb
|
35
|
+
lib/rkelly/nodes/function_decl_node.rb
|
36
|
+
lib/rkelly/nodes/function_expr_node.rb
|
37
|
+
lib/rkelly/nodes/if_node.rb
|
38
|
+
lib/rkelly/nodes/label_node.rb
|
39
|
+
lib/rkelly/nodes/new_expr_node.rb
|
40
|
+
lib/rkelly/nodes/node.rb
|
41
|
+
lib/rkelly/nodes/not_strict_equal_node.rb
|
42
|
+
lib/rkelly/nodes/op_equal_node.rb
|
43
|
+
lib/rkelly/nodes/postfix_node.rb
|
44
|
+
lib/rkelly/nodes/prefix_node.rb
|
45
|
+
lib/rkelly/nodes/property_node.rb
|
46
|
+
lib/rkelly/nodes/resolve_node.rb
|
47
|
+
lib/rkelly/nodes/strict_equal_node.rb
|
48
|
+
lib/rkelly/nodes/try_node.rb
|
49
|
+
lib/rkelly/nodes/var_decl_node.rb
|
50
|
+
lib/rkelly/parser.rb
|
51
|
+
lib/rkelly/runtime.rb
|
52
|
+
lib/rkelly/runtime/ruby_function.rb
|
53
|
+
lib/rkelly/runtime/scope_chain.rb
|
54
|
+
lib/rkelly/token.rb
|
55
|
+
lib/rkelly/tokenizer.rb
|
56
|
+
lib/rkelly/visitable.rb
|
57
|
+
lib/rkelly/visitors.rb
|
58
|
+
lib/rkelly/visitors/dot_visitor.rb
|
59
|
+
lib/rkelly/visitors/ecma_visitor.rb
|
60
|
+
lib/rkelly/visitors/enumerable_visitor.rb
|
61
|
+
lib/rkelly/visitors/evaluation_visitor.rb
|
62
|
+
lib/rkelly/visitors/function_visitor.rb
|
63
|
+
lib/rkelly/visitors/pointcut_visitor.rb
|
64
|
+
lib/rkelly/visitors/real_sexp_visitor.rb
|
65
|
+
lib/rkelly/visitors/sexp_visitor.rb
|
66
|
+
lib/rkelly/visitors/visitor.rb
|
67
|
+
test/ecma_script_test_case.rb
|
68
|
+
test/execute_test_case.rb
|
69
|
+
test/execution_contexts/test_10_1_3-1.rb
|
70
|
+
test/expressions/test_11_3_1.rb
|
71
|
+
test/expressions/test_11_3_2.rb
|
72
|
+
test/expressions/test_11_4_2.rb
|
73
|
+
test/expressions/test_11_4_3.rb
|
74
|
+
test/expressions/test_11_4_4.rb
|
75
|
+
test/expressions/test_11_4_5.rb
|
76
|
+
test/expressions/test_11_4_6.rb
|
77
|
+
test/expressions/test_11_4_8.rb
|
78
|
+
test/expressions/test_11_4_9.rb
|
79
|
+
test/expressions/test_11_5_1.rb
|
80
|
+
test/expressions/test_11_5_2.rb
|
81
|
+
test/expressions/test_11_5_3.rb
|
82
|
+
test/expressions/test_11_6_1-1.rb
|
83
|
+
test/expressions/test_11_9_1.rb
|
84
|
+
test/function/test_15_3_1_1-1.rb
|
85
|
+
test/global_object/test_15_1_1_1.rb
|
86
|
+
test/global_object/test_15_1_1_2.rb
|
87
|
+
test/global_object/test_15_1_1_3.rb
|
88
|
+
test/helper.rb
|
89
|
+
test/node_test_case.rb
|
90
|
+
test/object/test_15_2_1_1.rb
|
91
|
+
test/object/test_15_2_1_2.rb
|
92
|
+
test/object/test_15_2_2_1.rb
|
93
|
+
test/statements/test_12_5-1.rb
|
94
|
+
test/test_add_node.rb
|
95
|
+
test/test_arguments_node.rb
|
96
|
+
test/test_array_node.rb
|
97
|
+
test/test_assign_expr_node.rb
|
98
|
+
test/test_automatic_semicolon_insertion.rb
|
99
|
+
test/test_bit_and_node.rb
|
100
|
+
test/test_bit_or_node.rb
|
101
|
+
test/test_bit_x_or_node.rb
|
102
|
+
test/test_bitwise_not_node.rb
|
103
|
+
test/test_block_node.rb
|
104
|
+
test/test_bracket_accessor_node.rb
|
105
|
+
test/test_break_node.rb
|
106
|
+
test/test_case_block_node.rb
|
107
|
+
test/test_case_clause_node.rb
|
108
|
+
test/test_comma_node.rb
|
109
|
+
test/test_comments.rb
|
110
|
+
test/test_conditional_node.rb
|
111
|
+
test/test_const_statement_node.rb
|
112
|
+
test/test_continue_node.rb
|
113
|
+
test/test_delete_node.rb
|
114
|
+
test/test_divide_node.rb
|
115
|
+
test/test_do_while_node.rb
|
116
|
+
test/test_dot_accessor_node.rb
|
117
|
+
test/test_ecma_visitor.rb
|
118
|
+
test/test_element_node.rb
|
119
|
+
test/test_empty_statement_node.rb
|
120
|
+
test/test_equal_node.rb
|
121
|
+
test/test_evaluation_visitor.rb
|
122
|
+
test/test_expression_statement_node.rb
|
123
|
+
test/test_false_node.rb
|
124
|
+
test/test_for_in_node.rb
|
125
|
+
test/test_for_node.rb
|
126
|
+
test/test_function_body_node.rb
|
127
|
+
test/test_function_call_node.rb
|
128
|
+
test/test_function_decl_node.rb
|
129
|
+
test/test_function_expr_node.rb
|
130
|
+
test/test_function_visitor.rb
|
131
|
+
test/test_getter_property_node.rb
|
132
|
+
test/test_global_object.rb
|
133
|
+
test/test_greater_node.rb
|
134
|
+
test/test_greater_or_equal_node.rb
|
135
|
+
test/test_if_node.rb
|
136
|
+
test/test_in_node.rb
|
137
|
+
test/test_instance_of_node.rb
|
138
|
+
test/test_label_node.rb
|
139
|
+
test/test_left_shift_node.rb
|
140
|
+
test/test_less_node.rb
|
141
|
+
test/test_less_or_equal_node.rb
|
142
|
+
test/test_line_number.rb
|
143
|
+
test/test_logical_and_node.rb
|
144
|
+
test/test_logical_not_node.rb
|
145
|
+
test/test_logical_or_node.rb
|
146
|
+
test/test_modulus_node.rb
|
147
|
+
test/test_multiply_node.rb
|
148
|
+
test/test_new_expr_node.rb
|
149
|
+
test/test_not_equal_node.rb
|
150
|
+
test/test_not_strict_equal_node.rb
|
151
|
+
test/test_null_node.rb
|
152
|
+
test/test_number_node.rb
|
153
|
+
test/test_object_literal_node.rb
|
154
|
+
test/test_op_and_equal_node.rb
|
155
|
+
test/test_op_divide_equal_node.rb
|
156
|
+
test/test_op_equal_node.rb
|
157
|
+
test/test_op_l_shift_equal_node.rb
|
158
|
+
test/test_op_minus_equal_node.rb
|
159
|
+
test/test_op_mod_equal_node.rb
|
160
|
+
test/test_op_multiply_equal_node.rb
|
161
|
+
test/test_op_or_equal_node.rb
|
162
|
+
test/test_op_plus_equal_node.rb
|
163
|
+
test/test_op_r_shift_equal_node.rb
|
164
|
+
test/test_op_u_r_shift_equal_node.rb
|
165
|
+
test/test_op_x_or_equal_node.rb
|
166
|
+
test/test_parameter_node.rb
|
167
|
+
test/test_parser.rb
|
168
|
+
test/test_pointcut_visitor.rb
|
169
|
+
test/test_postfix_node.rb
|
170
|
+
test/test_prefix_node.rb
|
171
|
+
test/test_property_node.rb
|
172
|
+
test/test_regexp_node.rb
|
173
|
+
test/test_resolve_node.rb
|
174
|
+
test/test_return_node.rb
|
175
|
+
test/test_right_shift_node.rb
|
176
|
+
test/test_rkelly.rb
|
177
|
+
test/test_runtime.rb
|
178
|
+
test/test_scope_chain.rb
|
179
|
+
test/test_setter_property_node.rb
|
180
|
+
test/test_source_elements.rb
|
181
|
+
test/test_strict_equal_node.rb
|
182
|
+
test/test_string_node.rb
|
183
|
+
test/test_subtract_node.rb
|
184
|
+
test/test_switch_node.rb
|
185
|
+
test/test_this_node.rb
|
186
|
+
test/test_throw_node.rb
|
187
|
+
test/test_tokenizer.rb
|
188
|
+
test/test_true_node.rb
|
189
|
+
test/test_try_node.rb
|
190
|
+
test/test_type_of_node.rb
|
191
|
+
test/test_unary_minus_node.rb
|
192
|
+
test/test_unary_plus_node.rb
|
193
|
+
test/test_unsigned_right_shift_node.rb
|
194
|
+
test/test_var_decl_node.rb
|
195
|
+
test/test_var_statement_node.rb
|
196
|
+
test/test_void_node.rb
|
197
|
+
test/test_while_node.rb
|
198
|
+
test/test_with_node.rb
|