mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
compile scripts via maybe apis
This adds the ability to compile and run an script, passing in the ScriptOrigin so that it can track file information.
This commit is contained in:
parent
905037ea50
commit
e1c8f9ef5f
7 changed files with 55 additions and 35 deletions
|
@ -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 */
|
||||
|
|
|
@ -62,10 +62,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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue