Class: Johnson::SpiderMonkey::RubyLandProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/johnson/spidermonkey/ruby_land_proxy.rb,
ext/spidermonkey/ruby_land_proxy.c

Overview

native

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/johnson/spidermonkey/ruby_land_proxy.rb', line 32

def method_missing(sym, *args, &block)
  args << block if block_given?
  
  name = sym.to_s
  assignment = "=" == name[-1, 1]
  
  # default behavior if the slot's not there
  return super unless assignment || respond_to?(sym)
  
  unless function_property?(name)
    # for arity 0, treat it as a get
    return self[name] if args.empty?

    # arity 1 and quacking like an assignment, treat it as a set
    return self[name[0..-2]] = args[0] if assignment && 1 == args.size
  end        
  
  # okay, must really be a function
  call_function_property(name, *args)
end

Instance Method Details

#[](name) ⇒ Object

Returns the property with name.



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
# File 'ext/spidermonkey/ruby_land_proxy.c', line 50

static VALUE
get(VALUE self, VALUE name)
{
  RubyLandProxy* proxy;
  Data_Get_Struct(self, RubyLandProxy, proxy);

  JSContext * context = johnson_get_current_context(proxy->runtime);
  PREPARE_RUBY_JROOTS(context, 1);
  
  jsval proxy_value;
  JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
  JROOT(proxy_value);

  jsval js_value;  

  switch(TYPE(name)) {
    case T_FIXNUM:
      JCHECK(JS_GetElement(context,
          JSVAL_TO_OBJECT(proxy_value), (jsint)(NUM2INT(name)), &js_value));
      break;
    default:
      Check_Type(name, T_STRING);
      JCHECK(JS_GetProperty(context,
          JSVAL_TO_OBJECT(proxy_value), StringValueCStr(name), &js_value));
      break;
  }

  JRETURN_RUBY(CONVERT_TO_RUBY(proxy->runtime, js_value));
}

#[]=(name, value) ⇒ Object

Sets the property with name to value.



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
# File 'ext/spidermonkey/ruby_land_proxy.c', line 86

static VALUE
set(VALUE self, VALUE name, VALUE value)
{
  RubyLandProxy* proxy;
  Data_Get_Struct(self, RubyLandProxy, proxy);
  JSContext * context = johnson_get_current_context(proxy->runtime);
  
  PREPARE_RUBY_JROOTS(context, 2);
  
  jsval proxy_value;
  JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
  JROOT(proxy_value);

  jsval js_value;
  JCHECK(convert_to_js(proxy->runtime, value, &js_value));
  
  JROOT(js_value);

  switch(TYPE(name)) {
    case T_FIXNUM:
      JCHECK(JS_SetElement(context,
              JSVAL_TO_OBJECT(proxy_value), (jsint)(NUM2INT(name)), &js_value));
      break;
    default:
      Check_Type(name, T_STRING);
      JCHECK(JS_SetProperty(context,
            JSVAL_TO_OBJECT(proxy_value), StringValueCStr(name), &js_value));
      break;
  }

  JRETURN_RUBY(value);
}

#call(*args) ⇒ Object



20
21
22
# File 'lib/johnson/spidermonkey/ruby_land_proxy.rb', line 20

def call(*args)
  call_using(runtime.global, *args)
end

#call_using(this, *args) ⇒ Object



24
25
26
# File 'lib/johnson/spidermonkey/ruby_land_proxy.rb', line 24

def call_using(this, *args)
  native_call(this, *args)
end

#each {|obj| ... } ⇒ Object

Calls block with each item in the collection.

Yields:

  • (obj)


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'ext/spidermonkey/ruby_land_proxy.c', line 224

static VALUE
each(VALUE self)
{
  RubyLandProxy* proxy;
  Data_Get_Struct(self, RubyLandProxy, proxy);
  JSContext * context = johnson_get_current_context(proxy->runtime);
  
  PREPARE_RUBY_JROOTS(context, 5);
  
  jsval proxy_value;
  JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
  JROOT(proxy_value);

  JSObject* value = JSVAL_TO_OBJECT(proxy_value);
  JROOT(value);
  
  // arrays behave like you'd expect, indexes in order
  if (JS_IsArrayObject(context, value))
  {
    jsuint length;
    JCHECK(JS_GetArrayLength(context, value, &length));
    
    jsuint i = 0;
    for (i = 0; i < length; ++i)
    {
      jsval element;
      JCHECK(JS_GetElement(context, value, (signed) i, &element));
      CALL_RUBY_WRAPPER(rb_yield, convert_to_ruby(proxy->runtime, element));
    }
  }
  else
  {
    // not an array? behave like each on Hash; yield [key, value]
    JSIdArray* ids = JS_Enumerate(context, value);
    JCHECK(ids);

    JCLEANUP(destroy_id_array, ids);

    int i;
    for (i = 0; i < ids->length; ++i)
    {
      jsval js_key, js_value;

      JCHECK(JS_IdToValue(context, ids->vector[i], &js_key));
      JROOT(js_key);

      if (JSVAL_IS_STRING(js_key))
      {
        // regular properties have string keys
        JCHECK(JS_GetProperty(context, value,
          JS_GetStringBytes(JSVAL_TO_STRING(js_key)), &js_value));
      }
      else
      {
        // it's a numeric property, use array access
        JCHECK(JS_GetElement(context, value,
          JSVAL_TO_INT(js_key), &js_value));
      }
      JROOT(js_value);

      VALUE key = CONVERT_TO_RUBY(proxy->runtime, js_key);
      VALUE value = CONVERT_TO_RUBY(proxy->runtime, js_value);

      CALL_RUBY_WRAPPER(rb_yield, rb_ary_new3(2L, key, value));

      JUNROOT(js_value);
      JUNROOT(js_key);
    }
  }

  JRETURN_RUBY(self);
}

#function?Boolean

Returns true if this is a function.

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
134
135
136
# File 'ext/spidermonkey/ruby_land_proxy.c', line 125

static VALUE
function_p(VALUE self)
{
  RubyLandProxy* proxy;
  Data_Get_Struct(self, RubyLandProxy, proxy);
  JSContext * context = johnson_get_current_context(proxy->runtime);
  PREPARE_RUBY_JROOTS(context, 1);
  jsval proxy_value;
  JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
  JROOT(proxy_value);
  JRETURN_RUBY(JS_TypeOfValue(context, proxy_value) == JSTYPE_FUNCTION ? Qtrue : Qfalse);
}

#inspectObject



28
29
30
# File 'lib/johnson/spidermonkey/ruby_land_proxy.rb', line 28

def inspect
  toString
end

#lengthObject Also known as: size

Returns the length of the collection.



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'ext/spidermonkey/ruby_land_proxy.c', line 303

static VALUE
length(VALUE self)
{
  RubyLandProxy* proxy;
  Data_Get_Struct(self, RubyLandProxy, proxy);
  JSContext * context = johnson_get_current_context(proxy->runtime);

  PREPARE_RUBY_JROOTS(context, 2);
  
  jsval proxy_value;
  JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
  JROOT(proxy_value);

  JSObject* value = JSVAL_TO_OBJECT(proxy_value);
  JROOT(value);
  
  if (JS_IsArrayObject(context, value))
  {
    jsuint length;
    JCHECK(JS_GetArrayLength(context, value, &length));

    JRETURN_RUBY(INT2FIX(length));
  }
  else
  {
    JSIdArray* ids = JS_Enumerate(context, value);
    JCHECK(ids);
    VALUE length = INT2FIX(ids->length);
    
    JS_DestroyIdArray(context, ids);

    JRETURN_RUBY(length);
  }
}

#respond_to?(symbol) ⇒ Boolean

Returns true if obj responds to given method.

Returns:

  • (Boolean)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'ext/spidermonkey/ruby_land_proxy.c', line 144

static VALUE
respond_to_p(int argc, const VALUE* argv, VALUE self)
{
  VALUE sym, priv;

  rb_scan_args(argc, argv, "11", &sym, &priv);

  RubyLandProxy* proxy;
  Data_Get_Struct(self, RubyLandProxy, proxy);

  JSContext * context = johnson_get_current_context(proxy->runtime);
  PREPARE_RUBY_JROOTS(context, 2);
  
  VALUE stringval = rb_funcall(sym, rb_intern("to_s"), 0);
  char* name = StringValuePtr(stringval);
  
  // assignment is always okay
  if (name[strlen(name) - 1] == '=')
    JRETURN_RUBY(Qtrue);
  
  jsval proxy_value;
  JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
  JROOT(proxy_value);

  JSObject *obj;
  JSBool found;
  
  JCHECK(JS_ValueToObject(context, proxy_value, &obj));
  JROOT(obj);

  JCHECK(JS_HasProperty(context, obj, name, &found));

  JRETURN_RUBY(found ? Qtrue : CALL_RUBY_WRAPPER(rb_call_super, argc, argv));
}

#to_procObject



16
17
18
# File 'lib/johnson/spidermonkey/ruby_land_proxy.rb', line 16

def to_proc
  @proc ||= Proc.new { |*args| call(*args) }
end

#to_sObject

Converts object to a string.



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'ext/spidermonkey/ruby_land_proxy.c', line 429

static VALUE to_s(VALUE self)
{
  RubyLandProxy* proxy;
  Data_Get_Struct(self, RubyLandProxy, proxy);
  JSContext * context = johnson_get_current_context(proxy->runtime);

  PREPARE_RUBY_JROOTS(context, 1);
  
  jsval proxy_value;
  JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
  JROOT(proxy_value);
  
  JSString* str = JS_ValueToString(context, proxy_value);
  JRETURN_RUBY(convert_js_string_to_ruby(proxy->runtime, str));
}