Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit da152e3

Browse files
committed
Merge pull request #369 from cowboyd/4.5/script-compile-with-origin
compile scripts via maybe apis
2 parents 8b66c34 + 46713f8 commit da152e3

8 files changed

Lines changed: 61 additions & 42 deletions

File tree

ext/v8/maybe.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ namespace rr {
6666
// the underlying value.
6767
VALUE object;
6868
};
69+
70+
template <class T, class V>
71+
class MaybeLocal : public Maybe {
72+
public:
73+
MaybeLocal(v8::Isolate* isolate, v8::MaybeLocal<V> maybe) {
74+
if (!maybe.IsEmpty()) {
75+
just(T(isolate, maybe.ToLocalChecked()));
76+
}
77+
}
78+
};
6979
}
7080

7181
#endif /* RR_MAYBE_H */

ext/v8/object.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ namespace rr {
106106
return Function(isolate, handle.As<v8::Function>());
107107
}
108108

109+
if (handle->IsArray()) {
110+
return Array(isolate, handle.As<v8::Array>());
111+
}
112+
109113
// TODO: Enable this when the methods are implemented
110-
// if (handle->IsArray()) {
111-
// return Array((v8::Handle<v8::Array>)v8::Array::Cast(*handle));
112-
// }
113114
//
114115
// if (handle->IsDate()) {
115116
// // return Date(handle);

ext/v8/script-origin.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ namespace rr {
5757
Bool(origin.Options().IsOpaque()))) {
5858
}
5959

60+
inline operator v8::ScriptOrigin() {
61+
return v8::ScriptOrigin(
62+
Value(container->name),
63+
Integer(container->line_offset),
64+
Integer(container->column_offset),
65+
Boolean(container->is_shared_cross_origin),
66+
Integer(container->script_id),
67+
Boolean(container->is_embedder_debug_script),
68+
Value(container->source_map_url),
69+
Boolean(container->is_opaque));
70+
}
71+
6072
static void mark(Container* container) {
6173
rb_gc_mark(container->name);
6274
rb_gc_mark(container->line_offset);

ext/v8/script.cc

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,28 @@ namespace rr {
2323

2424

2525
VALUE Script::Compile(int argc, VALUE argv[], VALUE self) {
26-
VALUE source, rb_context, origin;
27-
rb_scan_args(argc, argv, "21", &source, &rb_context, &origin);
28-
29-
Context context(rb_context);
30-
Locker lock(context.getIsolate());
31-
32-
// TODO: ScriptOrigin
33-
return Script(context.getIsolate(), v8::Script::Compile(String(source)));
26+
VALUE r_source, r_context, r_origin;
27+
rb_scan_args(argc, argv, "21", &r_context, &r_source, &r_origin);
28+
29+
Context context(r_context);
30+
Isolate isolate(context.getIsolate());
31+
Locker lock(isolate);
32+
33+
34+
if (RTEST(r_origin)) {
35+
v8::ScriptOrigin origin = ScriptOrigin(r_origin);
36+
v8::MaybeLocal<v8::Script> script = v8::Script::Compile(context, String(r_source), &origin);
37+
return Script::Maybe(isolate, script);
38+
}
39+
else {
40+
return Script::Maybe(isolate, v8::Script::Compile(context, String(r_source)));
41+
}
3442
}
3543

3644
VALUE Script::Run(VALUE self, VALUE rb_context) {
3745
Context context(rb_context);
38-
Locker lock(context->GetIsolate());
46+
Locker lock(context);
3947

40-
return Value(context->GetIsolate(), Script(self)->Run());
48+
return Value::Maybe(context, Script(self)->Run(context));
4149
}
4250
}

ext/v8/script.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace rr {
1515

1616
inline Script(VALUE value) : Ref<v8::Script>(value) {}
1717
inline Script(v8::Isolate* isolate, v8::Handle<v8::Script> script) : Ref<v8::Script>(isolate, script) {}
18+
typedef MaybeLocal<Script, v8::Script> Maybe;
1819
};
1920
}
2021
#endif

ext/v8/value.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,7 @@ namespace rr {
66

77
class Value : public Ref<v8::Value> {
88
public:
9-
/**
10-
* A conversion class for down-thunking a MaybeLocal<v8::Value>
11-
* and returning it to Ruby as a `V8::C::Maybe`. If there is a
12-
* value present, then run it through the value conversion to get
13-
* the most specific subclass of Value:
14-
*
15-
* return Value::Maybe(object->Get(cxt, key));
16-
*/
17-
class Maybe : public rr::Maybe {
18-
public:
19-
Maybe(v8::Isolate* isolate, v8::MaybeLocal<v8::Value> maybe) {
20-
if (!maybe.IsEmpty()) {
21-
just(Value(isolate, maybe.ToLocalChecked()));
22-
}
23-
}
24-
};
9+
typedef MaybeLocal<Value, v8::Value> Maybe;
2510

2611
static void Init();
2712

spec/c/function_spec.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,9 @@ def call(info)
9090

9191
def run(source)
9292
source = V8::C::String.NewFromUtf8(@isolate, source.to_s)
93-
filename = V8::C::String.NewFromUtf8(@isolate, "<eval>")
94-
script = V8::C::Script.Compile(source, filename)
95-
result = script.Run(@ctx)
96-
97-
result.kind_of?(V8::C::String) ? result.Utf8Value : result
93+
script = V8::C::Script.Compile(@ctx, source)
94+
result = script.FromJust().Run(@ctx)
95+
checked = result.FromJust()
96+
checked.kind_of?(V8::C::String) ? checked.Utf8Value : checked
9897
end
9998
end

spec/c/script_spec.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
describe V8::C::Script do
44
requires_v8_context
55

6-
# TODO
7-
# it 'can run a script and return a polymorphic result' do
8-
# source = V8::C::String::New("(new Array())")
9-
# script = V8::C::Script::New(source)
10-
#
11-
# result = script.Run()
12-
# expect(result).to be_an V8::C::Array
13-
# end
6+
it 'can run a script and return a polymorphic result' do
7+
source = V8::C::String::NewFromUtf8(@isolate, "(new Array())")
8+
name = V8::C::String::NewFromUtf8(@isolate, "a/file.js")
9+
origin = V8::C::ScriptOrigin.new(name)
10+
script = V8::C::Script::Compile(@ctx, source, origin)
11+
expect(script.IsJust()).to be true
12+
13+
result = script.FromJust().Run(@ctx)
14+
expect(result.IsJust()).to be true
15+
expect(result.FromJust()).to be_an V8::C::Array
16+
end
1417

1518
# TODO
1619
# it 'can accept precompiled script data' do

0 commit comments

Comments
 (0)