Module: Kernel
- Defined in:
- opal/stdlib/pp.rb,
opal/stdlib/await.rb,
opal/stdlib/await.rb,
opal/stdlib/native.rb,
opal/stdlib/open-uri.rb,
opal/stdlib/pathname.rb,
opal/stdlib/bigdecimal.rb,
opal/stdlib/opal-parser.rb,
opal/stdlib/nodejs/kernel.rb,
opal/stdlib/nodejs/require.rb
Class Method Summary collapse
-
.open(name, *rest, &block) ⇒ Object
Allows the opening of various resources including URIs.
-
.open_uri_original_open ⇒ Object
:nodoc:.
-
.pp(*objs) ⇒ Object
prints arguments in pretty form.
Instance Method Summary collapse
- #__prepare_require__(path) ⇒ Object
- #_Array ⇒ Object
- #`(cmdline) ⇒ Object
-
#Array(object, *args, &block) ⇒ Object
Wraps array-like JavaScript objects in Native::Array.
- #BigDecimal(initial, digits = 0) ⇒ Object
- #eval(str, binding = nil, file = nil, line = nil) ⇒ Object
-
#exit(status = true) ⇒ Object
Overwrite Kernel.exit to be async-capable.
- #load(path) ⇒ Object
-
#Native(obj) ⇒ Native::Object, ...
Wraps a native JavaScript with
Native::Object.new
. - #native?(value) ⇒ Boolean
- #Pathname(path) ⇒ Object
-
#pretty_inspect ⇒ Object
Returns a pretty printed object as a string.
- #require(path) ⇒ Object
- #require_remote(url) ⇒ Object
- #sleep(seconds) ⇒ Object
- #system(*argv, exception: false) ⇒ Object
Class Method Details
.open(name, *rest, &block) ⇒ Object
Allows the opening of various resources including URIs.
If the first argument responds to the 'open' method, 'open' is called on it with the rest of the arguments.
If the first argument is a string that begins with xxx://, it is parsed by URI.parse. If the parsed object responds to the 'open' method, 'open' is called on it with the rest of the arguments.
Otherwise, the original Kernel#open is called.
OpenURI::OpenRead#open provides URI::HTTP#open, URI::HTTPS#open and URI::FTP#open, Kernel#open.
We can accept URIs and strings that begin with http://, https:// and ftp://. In these cases, the opened file object is extended by OpenURI::Meta.
29 30 31 32 33 34 35 |
# File 'opal/stdlib/open-uri.rb', line 29 def open(name, *rest, &block) # :doc: if name.respond_to?(:to_str) && %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name OpenURI.open_uri(name, *rest, &block) else open_uri_original_open(name, *rest, &block) end end |
.open_uri_original_open ⇒ Object
:nodoc:
10 |
# File 'opal/stdlib/open-uri.rb', line 10 alias open_uri_original_open open |
Instance Method Details
#__prepare_require__(path) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'opal/stdlib/nodejs/require.rb', line 4 def __prepare_require__(path) name = `Opal.normalize(#{path})` full_path = name.end_with?('.rb') ? name : name + '.rb' if `!Opal.modules[#{name}]` ruby = File.read(full_path) compiler = Opal::Compiler.new(ruby, requirable: true, file: name) js = compiler.compile compiler.requires.each do |sub_path| __prepare_require__(sub_path) end `eval(#{js})` end name rescue => e raise [path, name, full_path].inspect + e. end |
#`(cmdline) ⇒ Object
31 32 33 |
# File 'opal/stdlib/nodejs/kernel.rb', line 31 def `(cmdline) Buffer.new(`__child_process__.execSync(#{cmdline})`).to_s.encode('UTF-8') end |
#Array(object, *args, &block) ⇒ Object
Wraps array-like JavaScript objects in Native::Array
245 246 247 248 249 250 |
# File 'opal/stdlib/native.rb', line 245 def Array(object, *args, &block) if native?(object) return Native::Array.new(object, *args, &block).to_a end _Array(object) end |
#BigDecimal(initial, digits = 0) ⇒ Object
7 8 9 10 11 |
# File 'opal/stdlib/bigdecimal.rb', line 7 def BigDecimal(initial, digits = 0) bigdecimal = BigDecimal.allocate bigdecimal.initialize(initial, digits) bigdecimal end |
#eval(str, binding = nil, file = nil, line = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'opal/stdlib/opal-parser.rb', line 9 def eval(str, binding = nil, file = nil, line = nil) str = ::Opal.coerce_to!(str, String, :to_str) = { file: file || '(eval)', eval: true } = __OPAL_COMPILER_CONFIG__.merge() code = `Opal.compile(str, compiling_options)` if binding binding.js_eval(code) else %x{ return (function(self) { return eval(#{code}); })(self) } end end |
#exit(status = true) ⇒ Object
Overwrite Kernel.exit to be async-capable.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'opal/stdlib/await.rb', line 42 def exit(status = true) $__at_exit__ ||= [] until $__at_exit__.empty? block = $__at_exit__.pop block.call.__await__ end %x{ if (status.$$is_boolean) { status = status ? 0 : 1; } else { status = $coerce_to(status, #{Integer}, 'to_int') } Opal.exit(status); } nil end |
#load(path) ⇒ Object
27 28 29 |
# File 'opal/stdlib/nodejs/require.rb', line 27 def load(path) `Opal.load(#{__prepare_require__(path)})` end |
#Native(obj) ⇒ Native::Object, ...
Wraps a native JavaScript with Native::Object.new
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'opal/stdlib/native.rb', line 224 def Native(obj) if `#{obj} == null` nil elsif native?(obj) Native::Object.new(obj) elsif obj.is_a?(Array) obj.map do |o| Native(o) end elsif obj.is_a?(Proc) proc do |*args, &block| Native(obj.call(*args, &block)) end else obj end end |
#native?(value) ⇒ Boolean
215 216 217 |
# File 'opal/stdlib/native.rb', line 215 def native?(value) `value == null || !value.$$class` end |
#Pathname(path) ⇒ Object
222 223 224 |
# File 'opal/stdlib/pathname.rb', line 222 def Pathname(path) Pathname.new(path) end |
#pretty_inspect ⇒ Object
Returns a pretty printed object as a string.
In order to use this method you must first require the PP module:
require 'pp'
See the PP module for more information.
602 603 604 |
# File 'opal/stdlib/pp.rb', line 602 def pretty_inspect PP.pp(self, StringIO.new).string end |
#require(path) ⇒ Object
23 24 25 |
# File 'opal/stdlib/nodejs/require.rb', line 23 def require(path) `Opal.require(#{__prepare_require__(path)})` end |
#require_remote(url) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'opal/stdlib/opal-parser.rb', line 25 def require_remote(url) %x{ var r = new XMLHttpRequest(); r.open("GET", url, false); r.send(''); } eval `r.responseText` end |
#sleep(seconds) ⇒ Object
62 63 64 65 66 |
# File 'opal/stdlib/await.rb', line 62 def sleep(seconds) prom = PromiseV2.new `setTimeout(#{proc { prom.resolve }}, #{seconds * 1000})` prom end |
#system(*argv, exception: false) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'opal/stdlib/nodejs/kernel.rb', line 8 def system(*argv, exception: false) env = {} env = argv.shift if argv.first.is_a? Hash env = ENV.merge(env).to_n cmdname = argv.shift out = if argv.empty? `__child_process__.spawnSync(#{cmdname}, { shell: true, stdio: 'inherit', env: #{env} })` elsif Array === cmdname `__child_process__.spawnSync(#{cmdname[0]}, #{argv}, { argv0: #{cmdname[1]}, stdio: 'inherit', env: #{env} })` else `__child_process__.spawnSync(#{cmdname}, #{argv}, { stdio: 'inherit', env: #{env} })` end status = out.JS[:status] status = 127 if `status === null` pid = out.JS[:pid] $? = Process::Status.new(status, pid) raise "Command failed with exit #{status}: #{cmdname}" if exception && status != 0 status == 0 end |