opal 0.6.3 → 0.7.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/.spectator +2 -0
- data/.spectator-mspec +3 -0
- data/.travis.yml +8 -11
- data/CHANGELOG.md +33 -0
- data/CONTRIBUTING.md +8 -43
- data/Gemfile +15 -4
- data/Guardfile +77 -0
- data/README.md +15 -9
- data/Rakefile +36 -12
- data/benchmarks/operators.rb +11 -0
- data/bin/opal +10 -13
- data/bin/opal-build +4 -4
- data/bin/opal-mspec +10 -0
- data/bin/opal-repl +4 -3
- data/examples/sinatra/Gemfile +1 -1
- data/examples/sinatra/config.ru +3 -3
- data/lib/mspec/opal/main.rb.erb +2 -2
- data/lib/mspec/opal/rake_task.rb +31 -24
- data/lib/mspec/opal/runner.rb +18 -1
- data/lib/mspec/opal/sprockets.js +17 -0
- data/lib/opal.rb +1 -34
- data/lib/opal/builder.rb +92 -58
- data/lib/opal/builder_processors.rb +165 -0
- data/lib/opal/cli.rb +85 -144
- data/lib/opal/cli_options.rb +136 -90
- data/lib/opal/cli_runners.rb +10 -0
- data/lib/opal/cli_runners/nodejs.rb +56 -0
- data/lib/opal/cli_runners/phantom.js +35 -0
- data/lib/opal/cli_runners/phantomjs.rb +28 -0
- data/lib/opal/cli_runners/server.rb +54 -0
- data/lib/opal/compiler.rb +35 -16
- data/lib/opal/erb.rb +29 -15
- data/lib/opal/hike_path_finder.rb +18 -0
- data/lib/opal/nodes.rb +1 -0
- data/lib/opal/nodes/call.rb +107 -26
- data/lib/opal/nodes/call_special.rb +31 -6
- data/lib/opal/nodes/class.rb +2 -2
- data/lib/opal/nodes/constants.rb +5 -20
- data/lib/opal/nodes/def.rb +4 -4
- data/lib/opal/nodes/defined.rb +3 -3
- data/lib/opal/nodes/definitions.rb +1 -1
- data/lib/opal/nodes/for.rb +35 -0
- data/lib/opal/nodes/helpers.rb +2 -2
- data/lib/opal/nodes/iter.rb +3 -3
- data/lib/opal/nodes/literal.rb +10 -2
- data/lib/opal/nodes/masgn.rb +2 -2
- data/lib/opal/nodes/module.rb +2 -2
- data/lib/opal/nodes/scope.rb +1 -0
- data/lib/opal/nodes/singleton_class.rb +2 -2
- data/lib/opal/nodes/super.rb +2 -2
- data/lib/opal/nodes/top.rb +30 -3
- data/lib/opal/parser.rb +15 -1
- data/lib/opal/parser/grammar.rb +2571 -2452
- data/lib/opal/parser/grammar.y +37 -5
- data/lib/opal/parser/keywords.rb +2 -0
- data/lib/opal/parser/lexer.rb +21 -11
- data/lib/opal/path_reader.rb +28 -0
- data/lib/opal/paths.rb +38 -0
- data/lib/opal/source_map.rb +32 -15
- data/lib/opal/sprockets/environment.rb +9 -2
- data/lib/opal/sprockets/erb.rb +1 -2
- data/lib/opal/sprockets/path_reader.rb +34 -0
- data/lib/opal/sprockets/processor.rb +40 -39
- data/lib/opal/sprockets/server.rb +47 -33
- data/lib/opal/version.rb +1 -1
- data/opal.gemspec +10 -5
- data/opal/README.md +6 -0
- data/opal/corelib/array.rb +36 -4
- data/opal/corelib/array/inheritance.rb +6 -6
- data/opal/corelib/basic_object.rb +9 -9
- data/opal/corelib/boolean.rb +1 -1
- data/opal/corelib/class.rb +12 -12
- data/opal/corelib/dir.rb +20 -0
- data/opal/corelib/enumerable.rb +42 -42
- data/opal/corelib/enumerator.rb +1 -1
- data/opal/corelib/error.rb +2 -2
- data/opal/corelib/file.rb +56 -0
- data/opal/corelib/hash.rb +5 -5
- data/opal/corelib/helpers.rb +3 -3
- data/opal/corelib/io.rb +13 -10
- data/opal/corelib/kernel.rb +44 -68
- data/opal/corelib/method.rb +1 -1
- data/opal/corelib/module.rb +89 -114
- data/opal/corelib/nil_class.rb +1 -1
- data/opal/corelib/numeric.rb +27 -23
- data/opal/corelib/proc.rb +5 -5
- data/opal/corelib/range.rb +8 -4
- data/opal/corelib/regexp.rb +5 -5
- data/opal/corelib/runtime.js +589 -272
- data/opal/corelib/string.rb +52 -37
- data/opal/corelib/string/inheritance.rb +5 -5
- data/opal/corelib/time.rb +102 -52
- data/opal/corelib/variables.rb +3 -3
- data/opal/opal.rb +2 -0
- data/package.json +9 -0
- data/spec/filters/bugs/array.rb +0 -6
- data/spec/filters/bugs/language.rb +4 -0
- data/spec/filters/bugs/numeric.rb +7 -6
- data/spec/filters/bugs/opal.rb +2 -0
- data/spec/filters/bugs/regexp.rb +4 -0
- data/spec/filters/bugs/string.rb +0 -7
- data/spec/filters/bugs/stringscanner.rb +4 -1
- data/spec/filters/unsupported/private_methods.rb +2 -0
- data/spec/lib/builder_processors_spec.rb +27 -0
- data/spec/lib/builder_spec.rb +66 -0
- data/spec/{cli → lib}/cli_spec.rb +60 -5
- data/spec/{cli → lib}/compiler_spec.rb +66 -5
- data/spec/{cli → lib}/dependency_resolver_spec.rb +1 -1
- data/spec/lib/fixtures/no_requires.rb +1 -0
- data/spec/{cli → lib}/fixtures/opal_file.rb +0 -0
- data/spec/lib/fixtures/require_tree_test.rb +3 -0
- data/spec/lib/fixtures/required_tree_test/required_file1.rb +1 -0
- data/spec/lib/fixtures/required_tree_test/required_file2.rb +1 -0
- data/spec/lib/fixtures/requires.rb +7 -0
- data/spec/{cli → lib}/fixtures/sprockets_file.js.rb +0 -0
- data/spec/lib/fixtures/sprockets_require_tree_test.rb +3 -0
- data/spec/lib/hike_path_finder_spec.rb +23 -0
- data/spec/{cli → lib}/lexer_spec.rb +1 -1
- data/spec/{cli → lib}/parser/alias_spec.rb +1 -1
- data/spec/{cli → lib}/parser/and_spec.rb +1 -1
- data/spec/{cli → lib}/parser/attrasgn_spec.rb +1 -1
- data/spec/{cli → lib}/parser/begin_spec.rb +1 -1
- data/spec/{cli → lib}/parser/block_spec.rb +1 -1
- data/spec/{cli → lib}/parser/break_spec.rb +1 -1
- data/spec/{cli → lib}/parser/call_spec.rb +1 -1
- data/spec/{cli → lib}/parser/class_spec.rb +1 -1
- data/spec/{cli → lib}/parser/comments_spec.rb +1 -1
- data/spec/{cli → lib}/parser/def_spec.rb +1 -1
- data/spec/{cli → lib}/parser/if_spec.rb +1 -1
- data/spec/{cli → lib}/parser/iter_spec.rb +1 -1
- data/spec/{cli → lib}/parser/lambda_spec.rb +1 -1
- data/spec/{cli → lib}/parser/literal_spec.rb +1 -1
- data/spec/{cli → lib}/parser/masgn_spec.rb +1 -1
- data/spec/{cli → lib}/parser/module_spec.rb +1 -1
- data/spec/{cli → lib}/parser/not_spec.rb +1 -1
- data/spec/{cli → lib}/parser/op_asgn1_spec.rb +1 -1
- data/spec/{cli → lib}/parser/op_asgn2_spec.rb +1 -1
- data/spec/{cli → lib}/parser/or_spec.rb +1 -1
- data/spec/{cli → lib}/parser/return_spec.rb +1 -1
- data/spec/{cli → lib}/parser/sclass_spec.rb +1 -1
- data/spec/{cli → lib}/parser/string_spec.rb +8 -1
- data/spec/{cli → lib}/parser/super_spec.rb +1 -1
- data/spec/lib/parser/unary_spec.rb +48 -0
- data/spec/{cli → lib}/parser/undef_spec.rb +1 -1
- data/spec/{cli → lib}/parser/unless_spec.rb +1 -1
- data/spec/{cli → lib}/parser/variables_spec.rb +1 -1
- data/spec/{cli → lib}/parser/while_spec.rb +1 -1
- data/spec/{cli → lib}/parser/yield_spec.rb +1 -1
- data/spec/lib/path_reader_spec.rb +24 -0
- data/spec/lib/shared/path_finder_shared.rb +19 -0
- data/spec/lib/shared/path_reader_shared.rb +31 -0
- data/spec/lib/spec_helper.rb +9 -0
- data/spec/lib/sprockets/environment_spec.rb +30 -0
- data/spec/{cli → lib}/sprockets/erb_spec.rb +1 -1
- data/spec/lib/sprockets/path_reader_spec.rb +25 -0
- data/spec/{cli → lib}/sprockets/processor_spec.rb +9 -2
- data/spec/lib/sprockets/server_spec.rb +20 -0
- data/spec/opal/compiler/irb_spec.rb +11 -11
- data/spec/opal/core/fixtures/require_tree_files/file 1.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_files/file 2.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_files/file 3.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_files/file 4.rb +1 -0
- data/spec/opal/core/fixtures/require_tree_files/file 5.rb +1 -0
- data/spec/opal/core/kernel/require_tree_spec.rb +7 -0
- data/spec/opal/core/kernel/respond_to_spec.rb +2 -2
- data/spec/opal/core/runtime/method_missing_spec.rb +19 -0
- data/spec/opal/core/source_map_spec.rb +2 -2
- data/spec/opal/core/string_spec.rb +11 -0
- data/spec/opal/stdlib/erb/erb_spec.rb +0 -1
- data/spec/opal/stdlib/thread/mutex_spec.rb +40 -0
- data/spec/opal/stdlib/thread/thread_queue_spec.rb +32 -0
- data/spec/opal/stdlib/thread/thread_spec.rb +60 -0
- data/spec/rubyspecs +54 -11
- data/spec/spec_helper.rb +18 -3
- data/spec/support/mspec_rspec_adapter.rb +33 -0
- data/spec/{cli/spec_helper.rb → support/parser_helpers.rb} +10 -10
- data/stdlib/README.md +3 -0
- data/stdlib/benchmark.rb +10 -0
- data/stdlib/date.rb +2 -2
- data/stdlib/dir.rb +1 -5
- data/stdlib/file.rb +1 -7
- data/stdlib/json.rb +10 -1
- data/stdlib/native.rb +5 -5
- data/stdlib/nodejs.rb +5 -0
- data/stdlib/nodejs/dir.rb +13 -0
- data/stdlib/nodejs/file.rb +98 -0
- data/stdlib/nodejs/fileutils.rb +26 -0
- data/stdlib/nodejs/io.rb +2 -0
- data/stdlib/nodejs/irb.rb +45 -0
- data/stdlib/nodejs/process.rb +16 -0
- data/stdlib/nodejs/require.rb +32 -0
- data/stdlib/nodejs/rubygems.rb +68 -0
- data/stdlib/nodejs/runtime.rb +25 -0
- data/stdlib/nodejs/yaml.rb +11 -0
- data/stdlib/opal-parser.rb +1 -2
- data/stdlib/opal-source-maps.rb +2 -0
- data/stdlib/phantomjs.rb +8 -0
- data/stdlib/process.rb +10 -0
- data/stdlib/promise.rb +12 -4
- data/stdlib/set.rb +27 -0
- data/stdlib/source_map.rb +5 -63
- data/stdlib/source_map/map.rb +220 -0
- data/stdlib/source_map/mapping.rb +26 -0
- data/stdlib/source_map/offset.rb +88 -0
- data/stdlib/source_map/version.rb +3 -0
- data/stdlib/source_map/vlq.rb +77 -101
- data/stdlib/sourcemap.rb +1 -0
- data/stdlib/strscan.rb +7 -1
- data/stdlib/template.rb +1 -1
- data/stdlib/thread.rb +147 -7
- metadata +238 -104
- data/lib/mspec/opal/mspec_fixes.rb +0 -87
- data/spec/cli/sprockets/environment_spec.rb +0 -14
- data/spec/filters/bugs/symbol.rb +0 -5
- data/spec/opal/core/kernel/warn_spec.rb +0 -83
- data/spec/opal/core/language/numbers_spec.rb +0 -60
- data/stdlib/opal-source-maps.js.erb +0 -2
- data/stdlib/source_map/generator.rb +0 -251
- data/stdlib/source_map/parser.rb +0 -102
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'opal'
|
2
|
-
require 'file'
|
3
2
|
require 'set'
|
4
3
|
require 'opal-parser'
|
5
4
|
require 'mspec'
|
6
|
-
require 'mspec/
|
5
|
+
require 'mspec/version'
|
6
|
+
require 'support/mspec_rspec_adapter'
|
7
7
|
require 'mspec/opal/runner'
|
8
8
|
|
9
9
|
require 'math'
|
@@ -19,9 +19,24 @@ module Kernel
|
|
19
19
|
def eval_js(javascript)
|
20
20
|
`eval(javascript)`
|
21
21
|
end
|
22
|
+
|
23
|
+
def at_exit(&block)
|
24
|
+
$AT_EXIT_CALLBACKS ||= []
|
25
|
+
$AT_EXIT_CALLBACKS << block
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
if `!!window.OPAL_SPEC_PHANTOM`
|
30
|
+
require 'phantomjs'
|
31
|
+
formatter_class = PhantomFormatter
|
32
|
+
else
|
33
|
+
formatter_class = BrowserFormatter
|
22
34
|
end
|
23
35
|
|
24
|
-
|
36
|
+
# Uncomment the following to see example titles when they're executed.
|
37
|
+
# (useful to relate debug output to the example that generated it)
|
38
|
+
#
|
39
|
+
#formatter_class = PhantomDebugFormatter
|
25
40
|
|
26
41
|
# As soon as this file loads, tell the runner the specs are starting
|
27
42
|
OSpecRunner.main(formatter_class).will_start
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module MSpecRSpecAdapter
|
2
|
+
def expect(object)
|
3
|
+
MSpecRSpecAdapterShould.new(object)
|
4
|
+
end
|
5
|
+
|
6
|
+
def eq(expected)
|
7
|
+
MSpecRSpecAdapterEq.new(expected)
|
8
|
+
end
|
9
|
+
|
10
|
+
class MSpecRSpecAdapterEq < Struct.new(:object)
|
11
|
+
end
|
12
|
+
|
13
|
+
class MSpecRSpecAdapterShould < Struct.new(:object)
|
14
|
+
def to(expectation)
|
15
|
+
apply_expectation(:should, expectation)
|
16
|
+
end
|
17
|
+
|
18
|
+
def apply_expectation(type, expectation)
|
19
|
+
if MSpecRSpecAdapterEq === expectation
|
20
|
+
object.send(type) == expectation.object
|
21
|
+
else
|
22
|
+
object.send(type, expectation)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def not_to
|
27
|
+
apply_expectation(:should_not, expectation)
|
28
|
+
end
|
29
|
+
alias to_not not_to
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
include MSpecRSpecAdapter unless defined? RSpec
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require 'opal'
|
1
|
+
require 'opal/parser'
|
2
2
|
|
3
|
-
module
|
4
|
-
def parsed(source, file='(string)')
|
3
|
+
module ParserHelpers
|
4
|
+
def parsed(source, file='(ParserHelpers:string)')
|
5
5
|
Opal::Parser.new.parse(source, file)
|
6
6
|
end
|
7
7
|
|
@@ -27,11 +27,11 @@ module OpalSpecHelpers
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
config.include OpalSpecHelpers
|
30
|
+
if defined? RSpec
|
31
|
+
RSpec.configure do |config|
|
32
|
+
config.include ParserHelpers
|
33
|
+
end
|
34
|
+
else
|
35
|
+
include ParserHelpers
|
37
36
|
end
|
37
|
+
|
data/stdlib/README.md
ADDED
data/stdlib/benchmark.rb
ADDED
data/stdlib/date.rb
CHANGED
@@ -24,7 +24,7 @@ class Date
|
|
24
24
|
|
25
25
|
def -(date)
|
26
26
|
%x{
|
27
|
-
if (date
|
27
|
+
if (date.$$is_number) {
|
28
28
|
var result = #{clone};
|
29
29
|
result.date.setDate(#@date.getDate() - date);
|
30
30
|
return result;
|
@@ -40,7 +40,7 @@ class Date
|
|
40
40
|
|
41
41
|
def +(date)
|
42
42
|
%x{
|
43
|
-
if (date
|
43
|
+
if (date.$$is_number) {
|
44
44
|
var result = #{clone};
|
45
45
|
result.date.setDate(#@date.getDate() + date);
|
46
46
|
return result;
|
data/stdlib/dir.rb
CHANGED
data/stdlib/file.rb
CHANGED
data/stdlib/json.rb
CHANGED
@@ -20,7 +20,7 @@ module JSON
|
|
20
20
|
case 'object':
|
21
21
|
if (!value) return nil;
|
22
22
|
|
23
|
-
if (value
|
23
|
+
if (value.$$is_array) {
|
24
24
|
var arr = #{`options.array_class`.new};
|
25
25
|
|
26
26
|
for (var i = 0, ii = value.length; i < ii; i++) {
|
@@ -105,6 +105,14 @@ class Object
|
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
+
# BUG: Enumerable must come before Array, otherwise it overrides #to_json
|
109
|
+
# this is due to how modules are implemented.
|
110
|
+
module Enumerable
|
111
|
+
def to_json
|
112
|
+
to_a.to_json
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
108
116
|
class Array
|
109
117
|
def to_json
|
110
118
|
%x{
|
@@ -171,3 +179,4 @@ class Date
|
|
171
179
|
to_s
|
172
180
|
end
|
173
181
|
end
|
182
|
+
|
data/stdlib/native.rb
CHANGED
@@ -128,7 +128,7 @@ end
|
|
128
128
|
|
129
129
|
module Kernel
|
130
130
|
def native?(value)
|
131
|
-
`value == null || !value
|
131
|
+
`value == null || !value.$$class`
|
132
132
|
end
|
133
133
|
|
134
134
|
def Native(obj)
|
@@ -163,7 +163,7 @@ module Kernel
|
|
163
163
|
end
|
164
164
|
|
165
165
|
class Native::Object < BasicObject
|
166
|
-
include Native
|
166
|
+
include ::Native
|
167
167
|
|
168
168
|
def ==(other)
|
169
169
|
`#@native === #{Native.try_convert(other)}`
|
@@ -256,11 +256,11 @@ class Native::Object < BasicObject
|
|
256
256
|
alias kind_of? is_a?
|
257
257
|
|
258
258
|
def instance_of?(klass)
|
259
|
-
`self
|
259
|
+
`self.$$class === klass`
|
260
260
|
end
|
261
261
|
|
262
262
|
def class
|
263
|
-
`self
|
263
|
+
`self.$$class`
|
264
264
|
end
|
265
265
|
|
266
266
|
def to_a(options = {}, &block)
|
@@ -520,7 +520,7 @@ end
|
|
520
520
|
|
521
521
|
class Class
|
522
522
|
def native_alias(jsid, mid)
|
523
|
-
`#{self}
|
523
|
+
`#{self}.$$proto[#{jsid}] = #{self}.$$proto['$' + #{mid}]`
|
524
524
|
end
|
525
525
|
|
526
526
|
alias native_class native_module
|
data/stdlib/nodejs.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
class File < IO
|
2
|
+
include ::IO::Writable
|
3
|
+
include ::IO::Readable
|
4
|
+
|
5
|
+
@__fs__ = NodeJS.require :fs
|
6
|
+
@__path__ = NodeJS.require :path
|
7
|
+
`var __fs__ = #{@__fs__}`
|
8
|
+
`var __path__ = #{@__path__}`
|
9
|
+
|
10
|
+
def self.read path
|
11
|
+
`__fs__.readFileSync(#{path}).toString()`
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.exist? path
|
15
|
+
`__fs__.existsSync(#{path})`
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.realpath(pathname, dir_string = nil, cache = nil, &block)
|
19
|
+
pathname = join(dir_string, pathname) if dir_string
|
20
|
+
if block_given?
|
21
|
+
`
|
22
|
+
__fs__.realpath(#{pathname}, #{cache}, function(error, realpath){
|
23
|
+
if (error) #{raise error.message}
|
24
|
+
else #{block.call(`realpath`)}
|
25
|
+
})
|
26
|
+
`
|
27
|
+
else
|
28
|
+
`__fs__.realpathSync(#{pathname}, #{cache})`
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.basename(path, ext = undefined)
|
33
|
+
`__path__.basename(#{path}, #{ext})`
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.dirname(path)
|
37
|
+
`__path__.dirname(#{path})`
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.join(*paths)
|
41
|
+
`__path__.join.apply(__path__, #{paths})`
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.directory? path
|
45
|
+
return nil unless exist? path
|
46
|
+
`!!__fs__.lstatSync(path).isDirectory()`
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.file? path
|
50
|
+
return nil unless exist? path
|
51
|
+
`!!__fs__.lstatSync(path).isFile()`
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.size path
|
55
|
+
return nil unless exist? path
|
56
|
+
`__fs__.lstatSync(path).size`
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.open path, flags
|
60
|
+
file = new(path, flags)
|
61
|
+
if block_given?
|
62
|
+
begin
|
63
|
+
yield(file)
|
64
|
+
ensure
|
65
|
+
file.close
|
66
|
+
end
|
67
|
+
else
|
68
|
+
file
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
# Instance Methods
|
76
|
+
|
77
|
+
def initialize(path, flags)
|
78
|
+
flags = flags.gsub(/b/, '')
|
79
|
+
@path = path
|
80
|
+
@flags = flags
|
81
|
+
@fd = `__fs__.openSync(path, flags)`
|
82
|
+
end
|
83
|
+
|
84
|
+
attr_reader :path
|
85
|
+
|
86
|
+
def write string
|
87
|
+
`__fs__.writeSync(#{@fd}, #{string}, null, #{string}.length)`
|
88
|
+
end
|
89
|
+
|
90
|
+
def flush
|
91
|
+
`__fs__.fsyncSync(#@fd)`
|
92
|
+
end
|
93
|
+
|
94
|
+
def close
|
95
|
+
`__fs__.closeSync(#{@fd})`
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module FileUtils
|
2
|
+
extend self
|
3
|
+
`var __fs__ = #{File}.__fs__`
|
4
|
+
|
5
|
+
def mkdir_p path
|
6
|
+
return true if File.directory? path
|
7
|
+
`__fs__.mkdirSync(#{path})`
|
8
|
+
end
|
9
|
+
|
10
|
+
def cp source, target
|
11
|
+
target = File.join(target, File.basename(source)) if File.directory? target
|
12
|
+
`__fs__.writeFileSync(target, __fs__.readFileSync(source));`
|
13
|
+
end
|
14
|
+
|
15
|
+
def rm path
|
16
|
+
`__fs__.unlinkSync(path)`
|
17
|
+
end
|
18
|
+
|
19
|
+
def mv source, target
|
20
|
+
target = File.join(target, File.basename(source)) if File.directory? target
|
21
|
+
`__fs__.renameSync(source, target)`
|
22
|
+
end
|
23
|
+
|
24
|
+
alias mkpath mkdir_p
|
25
|
+
alias makedirs mkdir_p
|
26
|
+
end
|
data/stdlib/nodejs/io.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'native'
|
2
|
+
|
3
|
+
NodeRepl = Native(`OpalNode.node_require('repl')`)
|
4
|
+
|
5
|
+
def NodeRepl.start opations = {}
|
6
|
+
Native::Object.new(`#@native.start(#{opations.to_n})`)
|
7
|
+
end
|
8
|
+
|
9
|
+
line = 1
|
10
|
+
prompt_interrupted = false
|
11
|
+
|
12
|
+
prompt = ->(_self) {
|
13
|
+
tip = prompt_interrupted ? '*' : '>'
|
14
|
+
"irb(#{_self.to_s}):#{line.to_s.rjust(3, '0')}#{tip} "
|
15
|
+
}
|
16
|
+
|
17
|
+
$repl = NodeRepl.start prompt: prompt.call(self),
|
18
|
+
useGlobal: true, ignoreUndefined: true,
|
19
|
+
eval: -> (cmd, context, filename, callback) {
|
20
|
+
line += 1
|
21
|
+
|
22
|
+
cmd = cmd[1...-1].chomp
|
23
|
+
|
24
|
+
if cmd.empty?
|
25
|
+
prompt_interrupted = true
|
26
|
+
$repl.prompt = prompt.call(self)
|
27
|
+
|
28
|
+
callback.call('')
|
29
|
+
next
|
30
|
+
end
|
31
|
+
|
32
|
+
prompt_interrupted = false
|
33
|
+
$repl.prompt = prompt.call(self)
|
34
|
+
|
35
|
+
begin
|
36
|
+
result = `OpalNode.run(cmd, filename)`
|
37
|
+
result = nil if `#{result} == nil`
|
38
|
+
callback.call('=> '+result.inspect)
|
39
|
+
rescue => e
|
40
|
+
callback.call(e.backtrace.join("\n"))
|
41
|
+
end
|
42
|
+
}
|
43
|
+
|
44
|
+
# Add a newline before exiting
|
45
|
+
$repl.on :exit, -> { puts }
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Kernel
|
2
|
+
def exit
|
3
|
+
`process.exit()`
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
ARGV = `process.argv.slice(2)`
|
8
|
+
|
9
|
+
ENV = Object.new
|
10
|
+
def ENV.[]= name, value
|
11
|
+
`process.env[#{name.to_s}] = #{value.to_s}`
|
12
|
+
end
|
13
|
+
|
14
|
+
def ENV.[] name
|
15
|
+
`process.env[#{name}] || nil`
|
16
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'opal-parser'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
def __prepare_require__(path)
|
5
|
+
name = `$opal.normalize_loadable_path(#{path})`
|
6
|
+
full_path = name.end_with?('.rb') ? name : name+'.rb'
|
7
|
+
|
8
|
+
if `!$opal.modules[#{name}]`
|
9
|
+
ruby = File.read(full_path)
|
10
|
+
compiler = Opal::Compiler.new(ruby, requirable: true, file: name)
|
11
|
+
js = compiler.compile
|
12
|
+
compiler.requires.each do |sub_path|
|
13
|
+
__prepare_require__(sub_path)
|
14
|
+
end
|
15
|
+
`eval(#{js})`
|
16
|
+
end
|
17
|
+
|
18
|
+
name
|
19
|
+
rescue => e
|
20
|
+
raise [path, name, full_path].inspect+e.message
|
21
|
+
end
|
22
|
+
|
23
|
+
def require path
|
24
|
+
name = __prepare_require__(path)
|
25
|
+
`$opal.require(#{name})`
|
26
|
+
end
|
27
|
+
|
28
|
+
def load path
|
29
|
+
name = __prepare_require__(path)
|
30
|
+
`$opal.load(#{name})`
|
31
|
+
end
|
32
|
+
end
|