Module: Kernel

Defined in:
opal/stdlib/pp.rb,
opal/stdlib/native.rb,
opal/stdlib/pathname.rb,
opal/stdlib/opal-parser.rb,
opal/stdlib/nodejs/kernel.rb,
opal/stdlib/nodejs/require.rb

Constant Summary

NODE_REQUIRE =
`require`

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pp(*objs) ⇒ Object



6
7
8
9
10
11
# File 'opal/stdlib/pp.rb', line 6

def pp(*objs)
  objs.each {|obj|
    PP.pp(obj)
  }
  objs.size <= 1 ? objs.first : objs
end

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_loadable_path(#{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.message
end

#_ArrayObject



229
# File 'opal/stdlib/native.rb', line 229

alias_method :_Array, :Array

#Array(object, *args, &block) ⇒ Object

Public: Wraps array-like JavaScript objects in Native::Array



232
233
234
235
236
237
# File 'opal/stdlib/native.rb', line 232

def Array(object, *args, &block)
  if native?(object)
    return Native::Array.new(object, *args, &block).to_a
  end
  return _Array(object)
end

#callerObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'opal/stdlib/nodejs/kernel.rb', line 6

def caller
  %x{
    var stack;
    try {
      var err = Error("my error");
      throw err;
    } catch(e) {
      stack = e.stack;
    }
    return stack.$split("\n").slice(3);
  }
end

#eval(str) ⇒ Object



6
7
8
9
# File 'opal/stdlib/opal-parser.rb', line 6

def eval(str)
  code = Opal.compile str, file: '(eval)'
  `eval(#{code})`
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) ⇒ Object

Public: Wraps a native JavaScript with Native::Object.new

Returns:

  1. The wrapped object if it is native
  2. nil for null and undefined
  3. The object itself if it's not native


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'opal/stdlib/native.rb', line 211

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

Returns:



201
202
203
# File 'opal/stdlib/native.rb', line 201

def native?(value)
  `value == null || !value.$$class`
end

#node_require(path) ⇒ Object



19
20
21
# File 'opal/stdlib/nodejs/kernel.rb', line 19

def node_require(path)
  `#{NODE_REQUIRE}(#{path.to_str})`
end

#Pathname(path) ⇒ Object



52
53
54
# File 'opal/stdlib/pathname.rb', line 52

def Pathname(path)
  Pathname.new(path)
end

#pretty_inspectObject



2
3
4
# File 'opal/stdlib/pp.rb', line 2

def pretty_inspect
  inspect
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



11
12
13
14
15
16
17
18
# File 'opal/stdlib/opal-parser.rb', line 11

def require_remote url
  %x{
    var r = new XMLHttpRequest();
    r.open("GET", url, false);
    r.send('');
  }
  eval `r.responseText`
end