Class: Mustang::V8::Function

Inherits:
Object show all
Defined in:
lib/mustang/v8/function.rb,
ext/v8/v8_function.cpp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#<=>, #[], #[]=, #delegate, #each, #get, #keys, #method_missing, native_new, #properties, #respond_to?, #set, #to_hash

Methods included from Delegated

#method_missing, #old_method_missing, #old_respond_to?, #respond_to?, #to_s

Methods inherited from Value

#==, #===, #array?, #ary?, #bool?, #boolean?, #date?, #empty?, #external?, #false?, #func?, #function?, #int?, #integer?, #null?, #num?, #number?, #obj?, #object?, #regex?, #regexp?, #str?, #string?, #to_boolean, #to_integer, #to_number, #to_object, #to_string, #true?, #undefined?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mustang::V8::Object

Instance Attribute Details

#originObject (readonly)

#receiverObject (readonly)

Class Method Details

.V8::Function.new(proc) ⇒ Object .V8::Function.new { ... } ⇒ Object

When block given then creates new function based on it, otherwise creates function based on passed parameter (proc/lambda/method).

Overloads:

  • .V8::Function.new { ... } ⇒ Object

    Yields:



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
# File 'ext/v8/v8_function.cpp', line 61

static VALUE rb_v8_function_new(int argc, VALUE *argv, VALUE klass)
{
  HandleScope scope;
  PREVENT_CREATION_WITHOUT_CONTEXT();

  VALUE orig;
  
  if (rb_block_given_p()) {
    orig = rb_block_proc();
  } else {
    if (argc == 1) {
      orig = argv[0];
    } else {
      rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
      return Qnil;
    }
  }

  VALUE self = v8_ref_new(klass, to_v8_function(orig), orig);
  rb_iv_set(self, "@origin", orig);
  rb_iv_set(self, "@receiver", Qnil);
  v8_set_peer(self);

  return self;
}

Instance Method Details

#bind(obj) ⇒ Object

Binds given object as function’s receiver.

Returns:



160
161
162
163
# File 'ext/v8/v8_function.cpp', line 160

static VALUE rb_v8_function_bind(VALUE self, VALUE recv)
{
  return rb_iv_set(self, "@receiver", recv);
}

#call(*args, &block) ⇒ Object



4
5
6
# File 'lib/mustang/v8/function.rb', line 4

def call(*args, &block)
  call_on(@receiver, *args, &block);
end

#call_on(recv, *args) ⇒ Object

Executes function with given arguments on specified receiver. When function code is broken then proper JavaScript error will be returned.



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
124
# File 'ext/v8/v8_function.cpp', line 95

static VALUE rb_v8_function_call_on(int argc, VALUE *argv, VALUE self)
{
  HandleScope scope;
  TryCatch try_catch;

  if (argc < 1) {
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", FIX2INT(argc));
    return Qnil;
  }
  
  VALUE recv = argv[0];
  
  Handle<Object> this_obj =
    NIL_P(recv) ? Context::GetEntered()->Global() :
    unwrap(recv)->ToObject();
  
  Handle<Value> args[argc-1];
  
  for (int i = 1; i < argc; i++) {
    args[i-1] = to_v8(argv[i]);
  }
  
  Handle<Value> result = unwrap(self)->Call(this_obj, argc-1, args);

  if (try_catch.HasCaught()) {
    return rb_v8_error_new3(try_catch);
  } else {
    return to_ruby(result);
  }
}

#nameString

Returns function’s internal name.

Returns:



133
134
135
136
137
# File 'ext/v8/v8_function.cpp', line 133

static VALUE rb_v8_function_get_name(VALUE self)
{
  HandleScope scope;
  return to_ruby(unwrap(self)->GetName());
}

#name=(str) ⇒ String

Sets function’s internal name.

Returns:



146
147
148
149
150
151
# File 'ext/v8/v8_function.cpp', line 146

static VALUE rb_v8_function_set_name(VALUE self, VALUE name)
{
  HandleScope scope;
  unwrap(self)->SetName(String::New(StringValuePtr(name)));
  return name;
}

#to_procObject



8
9
10
# File 'lib/mustang/v8/function.rb', line 8

def to_proc
  origin.to_proc if origin
end