1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

Merge pull request #369 from cowboyd/4.5/script-compile-with-origin

compile scripts via maybe apis
This commit is contained in:
Charles Lowell 2015-07-23 14:12:33 -04:00
commit da152e3b78
8 changed files with 59 additions and 40 deletions

View file

@ -66,6 +66,16 @@ namespace rr {
// the underlying value.
VALUE object;
};
template <class T, class V>
class MaybeLocal : public Maybe {
public:
MaybeLocal(v8::Isolate* isolate, v8::MaybeLocal<V> maybe) {
if (!maybe.IsEmpty()) {
just(T(isolate, maybe.ToLocalChecked()));
}
}
};
}
#endif /* RR_MAYBE_H */

View file

@ -106,10 +106,11 @@ namespace rr {
return Function(isolate, handle.As<v8::Function>());
}
if (handle->IsArray()) {
return Array(isolate, handle.As<v8::Array>());
}
// TODO: Enable this when the methods are implemented
// if (handle->IsArray()) {
// return Array((v8::Handle<v8::Array>)v8::Array::Cast(*handle));
// }
//
// if (handle->IsDate()) {
// // return Date(handle);

View file

@ -57,6 +57,18 @@ namespace rr {
Bool(origin.Options().IsOpaque()))) {
}
inline operator v8::ScriptOrigin() {
return v8::ScriptOrigin(
Value(container->name),
Integer(container->line_offset),
Integer(container->column_offset),
Boolean(container->is_shared_cross_origin),
Integer(container->script_id),
Boolean(container->is_embedder_debug_script),
Value(container->source_map_url),
Boolean(container->is_opaque));
}
static void mark(Container* container) {
rb_gc_mark(container->name);
rb_gc_mark(container->line_offset);

View file

@ -23,20 +23,28 @@ namespace rr {
VALUE Script::Compile(int argc, VALUE argv[], VALUE self) {
VALUE source, rb_context, origin;
rb_scan_args(argc, argv, "21", &source, &rb_context, &origin);
VALUE r_source, r_context, r_origin;
rb_scan_args(argc, argv, "21", &r_context, &r_source, &r_origin);
Context context(rb_context);
Locker lock(context.getIsolate());
Context context(r_context);
Isolate isolate(context.getIsolate());
Locker lock(isolate);
// TODO: ScriptOrigin
return Script(context.getIsolate(), v8::Script::Compile(String(source)));
if (RTEST(r_origin)) {
v8::ScriptOrigin origin = ScriptOrigin(r_origin);
v8::MaybeLocal<v8::Script> script = v8::Script::Compile(context, String(r_source), &origin);
return Script::Maybe(isolate, script);
}
else {
return Script::Maybe(isolate, v8::Script::Compile(context, String(r_source)));
}
}
VALUE Script::Run(VALUE self, VALUE rb_context) {
Context context(rb_context);
Locker lock(context->GetIsolate());
Locker lock(context);
return Value(context->GetIsolate(), Script(self)->Run());
return Value::Maybe(context, Script(self)->Run(context));
}
}

View file

@ -15,6 +15,7 @@ namespace rr {
inline Script(VALUE value) : Ref<v8::Script>(value) {}
inline Script(v8::Isolate* isolate, v8::Handle<v8::Script> script) : Ref<v8::Script>(isolate, script) {}
typedef MaybeLocal<Script, v8::Script> Maybe;
};
}
#endif

View file

@ -6,22 +6,7 @@ namespace rr {
class Value : public Ref<v8::Value> {
public:
/**
* A conversion class for down-thunking a MaybeLocal<v8::Value>
* and returning it to Ruby as a `V8::C::Maybe`. If there is a
* value present, then run it through the value conversion to get
* the most specific subclass of Value:
*
* return Value::Maybe(object->Get(cxt, key));
*/
class Maybe : public rr::Maybe {
public:
Maybe(v8::Isolate* isolate, v8::MaybeLocal<v8::Value> maybe) {
if (!maybe.IsEmpty()) {
just(Value(isolate, maybe.ToLocalChecked()));
}
}
};
typedef MaybeLocal<Value, v8::Value> Maybe;
static void Init();

View file

@ -90,10 +90,9 @@ describe V8::C::Function do
def run(source)
source = V8::C::String.NewFromUtf8(@isolate, source.to_s)
filename = V8::C::String.NewFromUtf8(@isolate, "<eval>")
script = V8::C::Script.Compile(source, filename)
result = script.Run(@ctx)
result.kind_of?(V8::C::String) ? result.Utf8Value : result
script = V8::C::Script.Compile(@ctx, source)
result = script.FromJust().Run(@ctx)
checked = result.FromJust()
checked.kind_of?(V8::C::String) ? checked.Utf8Value : checked
end
end

View file

@ -3,14 +3,17 @@ require 'c_spec_helper'
describe V8::C::Script do
requires_v8_context
# TODO
# it 'can run a script and return a polymorphic result' do
# source = V8::C::String::New("(new Array())")
# script = V8::C::Script::New(source)
#
# result = script.Run()
# expect(result).to be_an V8::C::Array
# end
it 'can run a script and return a polymorphic result' do
source = V8::C::String::NewFromUtf8(@isolate, "(new Array())")
name = V8::C::String::NewFromUtf8(@isolate, "a/file.js")
origin = V8::C::ScriptOrigin.new(name)
script = V8::C::Script::Compile(@ctx, source, origin)
expect(script.IsJust()).to be true
result = script.FromJust().Run(@ctx)
expect(result.IsJust()).to be true
expect(result.FromJust()).to be_an V8::C::Array
end
# TODO
# it 'can accept precompiled script data' do