Class: V8::Function

Inherits:
Object show all
Defined in:
lib/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?

Methods inherited from Data

#empty?, #error?, #null?, #undefined?, #value?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class 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
# File 'ext/v8/v8_function.cpp', line 61

static VALUE rb_v8_function_new(int argc, VALUE *argv, VALUE klass)
{
  HandleScope scope;
  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);
  v8_set_peer(self);

  return self;
}

Instance Method Details

#bind(obj) ⇒ Object

Binds given object as function’s receiver.

Returns:



157
158
159
160
# File 'ext/v8/v8_function.cpp', line 157

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

#call(*args, &block) ⇒ Object



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

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.



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

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:



130
131
132
133
134
# File 'ext/v8/v8_function.cpp', line 130

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:



143
144
145
146
147
148
# File 'ext/v8/v8_function.cpp', line 143

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

#to_procObject



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

def to_proc
  origin.to_proc if origin
end