Class: Mustang::V8::Context

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorObject (readonly)

#errorsObject (readonly)

Class Method Details

.V8::Context.exit_all!nil

Exits from all entered context.

Returns:

  • (nil)


174
175
176
177
178
179
180
181
182
183
# File 'ext/v8/v8_context.cpp', line 174

static VALUE rb_v8_context_exit_all_bang(VALUE klass)
{
  HandleScope scope;

  while (Context::InContext()) {
    Context::GetEntered()->Exit();
  }

  return Qnil;
}

.V8::Context.newObject

Returns new V8 context.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'ext/v8/v8_context.cpp', line 23

static VALUE rb_v8_context_new(VALUE self)
{
  HandleScope scope;
  Persistent<Context> context(Context::New());

  VALUE ref = v8_ref_new(self, context);
  rb_v8_context_enter(ref);
  rb_iv_set(ref, "@errors", rb_ary_new());

  context.Dispose();
  return ref;
}

Instance Method Details

#enterBoolean #enter {|cxt| ... } ⇒ nil

Enters to context. Returns true when enter action has been performed. If current context is already entered then returns false.

If block passed then context enters only for block execution, and exits imidietely after that.

Overloads:

  • #enterBoolean

    Returns:

  • #enter {|cxt| ... } ⇒ nil

    Yields:

    • (cxt)

    Returns:

    • (nil)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'ext/v8/v8_context.cpp', line 49

static VALUE rb_v8_context_enter(VALUE self)
{
  HandleScope scope;
  Handle<Context> context = unwrap(self);

  VALUE entered = Qfalse;

  if (Context::GetEntered() != context) {
    context->Enter();
    entered = Qtrue;
  }
  
  if (rb_block_given_p()) {
    rb_yield(self);
    context->Exit();
    return Qnil; 
  }
  
  return entered;
}

#entered?Boolean

Returns true when this context is entered.

Returns:



142
143
144
145
146
# File 'ext/v8/v8_context.cpp', line 142

static VALUE rb_v8_context_entered_p(VALUE self)
{
  HandleScope scope;
  return unwrap(self) == Context::GetEntered() ? Qtrue : Qfalse;
}

#eval(source, filename) ⇒ Object #evaluate(source, filename) ⇒ Object

Evaluates given JavaScript code within current context.

cxt = V8::Context.new
cxt.evaluate("1+1", "script.js")    # => 2
cxt.evaluate("var a=1", "<eval>")   # => 1
cxt.evaluate("var b=a+1", "<eval>") # => 2


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/v8/v8_context.cpp', line 83

static VALUE rb_v8_context_evaluate(VALUE self, VALUE source, VALUE filename)
{
  HandleScope scope;
  
  Local<String> _source(String::Cast(*to_v8(source)));
  Local<String> _filename(String::Cast(*to_v8(filename)));

  rb_v8_context_enter(self);
  rb_iv_set(self, "@error", Qfalse);  

  TryCatch try_catch;
  Local<Script> script = Script::Compile(_source, _filename);

  if (!try_catch.HasCaught()) {
    Local<Value> result = script->Run();

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

  return rb_v8_error_new3(try_catch);
}

#eval(source, filename) ⇒ Object #evaluate(source, filename) ⇒ Object

Evaluates given JavaScript code within current context.

cxt = V8::Context.new
cxt.evaluate("1+1", "script.js")    # => 2
cxt.evaluate("var a=1", "<eval>")   # => 1
cxt.evaluate("var b=a+1", "<eval>") # => 2


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/v8/v8_context.cpp', line 83

static VALUE rb_v8_context_evaluate(VALUE self, VALUE source, VALUE filename)
{
  HandleScope scope;
  
  Local<String> _source(String::Cast(*to_v8(source)));
  Local<String> _filename(String::Cast(*to_v8(filename)));

  rb_v8_context_enter(self);
  rb_iv_set(self, "@error", Qfalse);  

  TryCatch try_catch;
  Local<Script> script = Script::Compile(_source, _filename);

  if (!try_catch.HasCaught()) {
    Local<Value> result = script->Run();

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

  return rb_v8_error_new3(try_catch);
}

#exitnil

Exits from context.

Returns:

  • (nil)


155
156
157
158
159
160
161
162
163
164
165
# File 'ext/v8/v8_context.cpp', line 155

static VALUE rb_v8_context_exit(VALUE self)
{
  HandleScope scope;
  Handle<Context> context;
  
  if (Context::InContext()) {
    context->Exit();
  }
  
  return Qnil;
}

#get(key) ⇒ Object Also known as: []



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

def get(key)
  prototype[key]
end

#globalObject

Returns global object for this context.



128
129
130
131
132
133
# File 'ext/v8/v8_context.cpp', line 128

static VALUE rb_v8_context_global(VALUE self)
{
  HandleScope scope;
  Handle<Value> global = unwrap(self)->Global();
  return to_ruby(global);
}

#prototypeObject

Returns prototype object of current context.

Returns:



114
115
116
117
118
119
# File 'ext/v8/v8_context.cpp', line 114

VALUE rb_v8_context_prototype(VALUE self)
{
  HandleScope scope;
  Handle<Object> proto(Object::Cast(*unwrap(self)->Global()->GetPrototype()));
  return to_ruby(proto);
}

#set(key, value) ⇒ Object Also known as: []=



9
10
11
# File 'lib/mustang/v8/context.rb', line 9

def set(key, value)
  prototype[key] = value
end