Class: RubyRunJs::BuiltInContext

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/ruby_run_js/builtin_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#check_object, #get_member, #get_member_dot, #is_accessor_descriptor, #is_callable, #is_data_descriptor, #is_generic_descriptor, #is_primitive, #make_error, #strict_equality

Methods included from ConversionHelper

#convert_to_js_type, #to_boolean, #to_int32, #to_integer, #to_number, #to_object, #to_primitive, #to_string, #to_uint16, #to_uint32

Constructor Details

#initializeBuiltInContext

Returns a new instance of BuiltInContext.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby_run_js/builtin_context.rb', line 14

def initialize

  @object_prototype = JsObject.new(nil)
  @function_prototype = JsFunction.new(proc { |*_| undefined }, nil, nil, 'Function', self, false, nil, @object_prototype)
  @array_prototype = JsArray.new(0, @object_prototype)
  @string_prototype = JsString.new('', @object_prototype)
  @boolean_prototype = JsBoolean.new(false, @object_prototype)
  @number_prototype = JsNumber.new(0.0, @object_prototype)
  @date_prototype = JsDate.new(Float::NAN, @object_prototype)
  @regexp_prototype = JsRegExp.new('', '', @object_prototype)
  @error_prototype = JsError.new('', @object_prototype)

  @math = JsMath.new(@object_prototype)
  @json = JsJson.new(@object_prototype)
  @global = GlobalScope.new(self)
  @global.this_binding = @global

  @object_constructor = new_construct_function(JsObjectMethods, 'Object')
  @function_constructor = new_construct_function(JsFunctionMethods, 'Function')
  @array_constructor = new_construct_function(JsArrayMethods, 'Array')
  @string_constructor = new_construct_function(JsStringMethods, 'String')
  @boolean_constructor = new_construct_function(JsBooleanMethods, 'Boolean')
  @number_constructor = new_construct_function(JsNumberMethods, 'Number')
  @date_constructor = new_construct_function(JsDateMethods, 'Date')
  @regexp_constructor = new_construct_function(JsRegExpMethods, 'RegExp')
  @error_constructor = new_construct_function(JsErrorMethods, 'Error')

  fill_constructor(@object_constructor, JsObjectMethods, 1, @object_prototype)
  fill_constructor(@function_constructor, JsFunctionMethods, 1, @function_prototype)
  set_freeze(@function_constructor, 'length', 1.0)

  fill_constructor(@array_constructor, JsArrayMethods, 1, @array_prototype)
  fill_constructor(@string_constructor, JsStringMethods, 1, @string_prototype)
  fill_constructor(@boolean_constructor, JsBooleanMethods, 1, @boolean_prototype)
  fill_constructor(@number_constructor, JsNumberMethods, 1, @number_prototype)
  fill_constructor(@date_constructor, JsDateMethods, 7, @date_prototype)
  fill_constructor(@regexp_constructor, JsRegExpMethods, 2, @regexp_prototype)
  fill_constructor(@error_constructor, JsErrorMethods, 1, @error_prototype)

  fill_constructor_with_properties(@math, JsMathMethods)
  fill_constructor_with_properties(@json, JsJsonMethods)
  fill_constructor_with_properties(@global, JsGlobalMethods)

  fill_prototype(@object_prototype, JsObjectMethods, @object_constructor)
  fill_prototype(@function_prototype, JsFunctionMethods, @function_constructor)
  set_freeze(@function_prototype, 'length', 0.0)

  fill_prototype(@array_prototype, JsArrayMethods, @array_constructor)
  fill_prototype(@string_prototype, JsStringMethods, @string_constructor)
  fill_prototype(@boolean_prototype, JsBooleanMethods, @boolean_constructor)
  fill_prototype(@number_prototype, JsNumberMethods, @number_constructor)
  fill_prototype(@date_prototype, JsDateMethods, @date_constructor)
  fill_prototype(@regexp_prototype, JsRegExpMethods, @regexp_constructor)
  fill_prototype(@error_prototype, JsErrorMethods, @error_constructor)

  set_non_enumerable(@error_prototype, 'name', 'Error')
  set_non_enumerable(@error_prototype, 'message', '')

  @native_error_constructors = {}
  @native_error_prototypes = {}

  ['EvalError', 'RangeError',\
  'ReferenceError', 'SyntaxError', 'TypeError',\
  'URIError'].each do |error_name|

    prototype = JsError.new('', @error_prototype)

    @native_error_prototypes[error_name] = prototype

    constructor_func = proc do |_, _, message|
      JsError.new(message == undefined ? message : to_string(message), prototype)
    end

    constructor = new_native_function(constructor_func, error_name)

    set_non_enumerable(constructor, '__new__', constructor)

    set_freeze(constructor, 'length', 1.0)
    set_freeze(constructor, 'prototype', prototype)

    set_non_enumerable(prototype, 'constructor', constructor)
    set_non_enumerable(prototype, 'name', error_name)
    set_non_enumerable(prototype, 'message', '')

    @native_error_constructors[error_name] = constructor

  end

  js_log_func = new_native_function(proc { |_, _, c| puts(c) }, 'log')

  console = new_object()
  console.put('log', js_log_func)

  set_non_enumerable(@global, 'Object', @object_constructor)
  set_non_enumerable(@global, 'Function', @function_constructor)
  set_non_enumerable(@global, 'Array', @array_constructor)
  set_non_enumerable(@global, 'String', @string_constructor)
  set_non_enumerable(@global, 'Boolean', @boolean_constructor)
  set_non_enumerable(@global, 'Number', @number_constructor)
  set_non_enumerable(@global, 'Date', @date_constructor)
  set_non_enumerable(@global, 'RegExp', @regexp_constructor)
  set_non_enumerable(@global, 'Error', @error_constructor)
  set_non_enumerable(@global, 'Math', @math)
  set_non_enumerable(@global, 'JSON', @json)
  set_non_enumerable(@global, 'console', console)
  
  @native_error_constructors.each_pair do |name, constructor|
    set_non_enumerable(@global, name, constructor)
  end
end

Instance Attribute Details

#array_constructorObject (readonly)

Returns the value of attribute array_constructor.



7
8
9
# File 'lib/ruby_run_js/builtin_context.rb', line 7

def array_constructor
  @array_constructor
end

#array_prototypeObject (readonly)

Returns the value of attribute array_prototype.



6
7
8
# File 'lib/ruby_run_js/builtin_context.rb', line 6

def array_prototype
  @array_prototype
end

#boolean_constructorObject (readonly)

Returns the value of attribute boolean_constructor.



7
8
9
# File 'lib/ruby_run_js/builtin_context.rb', line 7

def boolean_constructor
  @boolean_constructor
end

#boolean_prototypeObject (readonly)

Returns the value of attribute boolean_prototype.



6
7
8
# File 'lib/ruby_run_js/builtin_context.rb', line 6

def boolean_prototype
  @boolean_prototype
end

#date_constructorObject (readonly)

Returns the value of attribute date_constructor.



7
8
9
# File 'lib/ruby_run_js/builtin_context.rb', line 7

def date_constructor
  @date_constructor
end

#date_prototypeObject (readonly)

Returns the value of attribute date_prototype.



6
7
8
# File 'lib/ruby_run_js/builtin_context.rb', line 6

def date_prototype
  @date_prototype
end

#error_constructorObject (readonly)

Returns the value of attribute error_constructor.



7
8
9
# File 'lib/ruby_run_js/builtin_context.rb', line 7

def error_constructor
  @error_constructor
end

#error_prototypeObject (readonly)

Returns the value of attribute error_prototype.



6
7
8
# File 'lib/ruby_run_js/builtin_context.rb', line 6

def error_prototype
  @error_prototype
end

#executorObject

Returns the value of attribute executor.



10
11
12
# File 'lib/ruby_run_js/builtin_context.rb', line 10

def executor
  @executor
end

#funtion_constructorObject (readonly)

Returns the value of attribute funtion_constructor.



7
8
9
# File 'lib/ruby_run_js/builtin_context.rb', line 7

def funtion_constructor
  @funtion_constructor
end

#funtion_prototypeObject (readonly)

Returns the value of attribute funtion_prototype.



6
7
8
# File 'lib/ruby_run_js/builtin_context.rb', line 6

def funtion_prototype
  @funtion_prototype
end

#globalObject (readonly)

Returns the value of attribute global.



8
9
10
# File 'lib/ruby_run_js/builtin_context.rb', line 8

def global
  @global
end

#interpreterObject

Returns the value of attribute interpreter.



10
11
12
# File 'lib/ruby_run_js/builtin_context.rb', line 10

def interpreter
  @interpreter
end

#jsonObject (readonly)

Returns the value of attribute json.



8
9
10
# File 'lib/ruby_run_js/builtin_context.rb', line 8

def json
  @json
end

#mathObject (readonly)

Returns the value of attribute math.



8
9
10
# File 'lib/ruby_run_js/builtin_context.rb', line 8

def math
  @math
end

#number_constructorObject (readonly)

Returns the value of attribute number_constructor.



7
8
9
# File 'lib/ruby_run_js/builtin_context.rb', line 7

def number_constructor
  @number_constructor
end

#number_prototypeObject (readonly)

Returns the value of attribute number_prototype.



6
7
8
# File 'lib/ruby_run_js/builtin_context.rb', line 6

def number_prototype
  @number_prototype
end

#object_constructorObject (readonly)

Returns the value of attribute object_constructor.



7
8
9
# File 'lib/ruby_run_js/builtin_context.rb', line 7

def object_constructor
  @object_constructor
end

#object_prototypeObject (readonly)

Returns the value of attribute object_prototype.



6
7
8
# File 'lib/ruby_run_js/builtin_context.rb', line 6

def object_prototype
  @object_prototype
end

#regexp_constructorObject (readonly)

Returns the value of attribute regexp_constructor.



7
8
9
# File 'lib/ruby_run_js/builtin_context.rb', line 7

def regexp_constructor
  @regexp_constructor
end

#regexp_prototypeObject (readonly)

Returns the value of attribute regexp_prototype.



6
7
8
# File 'lib/ruby_run_js/builtin_context.rb', line 6

def regexp_prototype
  @regexp_prototype
end

#string_constructorObject (readonly)

Returns the value of attribute string_constructor.



7
8
9
# File 'lib/ruby_run_js/builtin_context.rb', line 7

def string_constructor
  @string_constructor
end

#string_prototypeObject (readonly)

Returns the value of attribute string_prototype.



6
7
8
# File 'lib/ruby_run_js/builtin_context.rb', line 6

def string_prototype
  @string_prototype
end

Instance Method Details

#new_arguments_obj(args) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ruby_run_js/builtin_context.rb', line 183

def new_arguments_obj(args)
  obj = new_object()
  obj._class = 'Arguments'
  obj.define_own_property('length', {
    'value' => args.length.to_f,
    'writable' => true,
    'enumerable' => false,
    'configurable' => true
  }, false)
  args.length.times do |i|
    obj.put(i.to_s, args[i])
  end
  obj
end

#new_array(length = 0) ⇒ Object



141
142
143
# File 'lib/ruby_run_js/builtin_context.rb', line 141

def new_array(length = 0)
  JsArray.new(length, @array_prototype)
end

#new_array_with_items(items) ⇒ Object



145
146
147
148
149
# File 'lib/ruby_run_js/builtin_context.rb', line 145

def new_array_with_items(items)
  arr = JsArray.new(0, @array_prototype)
  arr.set_items(items)
  arr
end

#new_boolean(value) ⇒ Object



159
160
161
# File 'lib/ruby_run_js/builtin_context.rb', line 159

def new_boolean(value)
  JsBoolean.new(value, @boolean_prototype)
end

#new_construct_function(js_class, name) ⇒ Object



133
134
135
# File 'lib/ruby_run_js/builtin_context.rb', line 133

def new_construct_function(js_class, name)
  new_native_function(js_class.method(:constructor), name)
end

#new_date(value) ⇒ Object



167
168
169
# File 'lib/ruby_run_js/builtin_context.rb', line 167

def new_date(value)
  JsDate.new(value, @date_prototype)
end

#new_date_by_ruby_time(rb_time) ⇒ Object



171
172
173
# File 'lib/ruby_run_js/builtin_context.rb', line 171

def new_date_by_ruby_time(rb_time)
  JsDate.new((rb_time.to_f.round(3) * 1000).to_i, @date_prototype)
end

#new_error(type, msg) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/ruby_run_js/builtin_context.rb', line 175

def new_error(type, msg)
  prototype = @error_prototype
  if @native_error_prototypes.key?(type)
    prototype = @native_error_prototypes[type]
  end
  JsError.new(msg, prototype)
end

#new_function(code, scope, params, name, is_declaraion, definitions) ⇒ Object



129
130
131
# File 'lib/ruby_run_js/builtin_context.rb', line 129

def new_function(code, scope, params, name, is_declaraion, definitions)
  JsFunction.new(code, scope, params, name, self, is_declaraion, definitions, @function_prototype)
end

#new_native_function(native_func, name) ⇒ Object



137
138
139
# File 'lib/ruby_run_js/builtin_context.rb', line 137

def new_native_function(native_func, name)
  new_function(native_func, nil, native_func.parameters[2..], name, false, nil)
end

#new_number(value) ⇒ Object



163
164
165
# File 'lib/ruby_run_js/builtin_context.rb', line 163

def new_number(value)
  JsNumber.new(value, @number_prototype)
end

#new_objectObject



125
126
127
# File 'lib/ruby_run_js/builtin_context.rb', line 125

def new_object()
  JsObject.new(@object_prototype)
end

#new_regexp(body, flags) ⇒ Object



155
156
157
# File 'lib/ruby_run_js/builtin_context.rb', line 155

def new_regexp(body, flags)
  JsRegExp.new(body, flags, @regexp_prototype)
end

#new_string(value = '') ⇒ Object



151
152
153
# File 'lib/ruby_run_js/builtin_context.rb', line 151

def new_string(value = '')
  JsString.new(value, @string_prototype)
end