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

Commit 5ac3483

Browse files
committed
Implement the rest of the Object mehods:
- GetConstructorName - InternalFieldCount - GetInternalField - SetInternalField - HasOwnProperty - HasRealNamedProperty - HasRealIndexedProperty - HasRealNamedCallbackProperty - GetRealNamedPropertyInPrototypeChain - GetRealNamedProperty - GetRealNamedPropertyAttributes - GetRealNamedPropertyAttributesInPrototypeChain - HasNamedLookupInterceptor - HasIndexedLookupInterceptor - SetHiddenValue - GetHiddenValue - DeleteHiddenValue - Clone - CreationContext - IsCallable - CallAsFunction - CallAsConstructor
1 parent 200d617 commit 5ac3483

4 files changed

Lines changed: 388 additions & 5 deletions

File tree

ext/v8/object-template.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ namespace rr {
88
ObjectTemplate(VALUE self) : Ref<v8::ObjectTemplate>(self) {}
99
ObjectTemplate(v8::Isolate* isolate, v8::Handle<v8::ObjectTemplate> tmpl) :
1010
Ref<v8::ObjectTemplate>(isolate, tmpl) {}
11+
1112
inline static void Init() {
1213
ClassBuilder("ObjectTemplate", Template::Class).
1314
defineSingletonMethod("New", &New).
1415
defineMethod("NewInstance", &NewInstance).
16+
defineMethod("SetInternalFieldCount", &SetInternalFieldCount).
1517
store(&Class);
1618
}
1719

@@ -20,16 +22,26 @@ namespace rr {
2022
rb_scan_args(argc, argv, "11", &r_isolate, &r_constructor);
2123
Isolate isolate(r_isolate);
2224
Locker lock(isolate);
25+
2326
return ObjectTemplate(isolate, v8::ObjectTemplate::New(isolate));
2427
}
2528

26-
static VALUE NewInstance(VALUE self , VALUE r_context) {
29+
static VALUE NewInstance(VALUE self, VALUE r_context) {
2730
ObjectTemplate t(self);
2831
Context context(r_context);
2932
Isolate isolate(context.getIsolate());
3033
Locker lock(isolate);
31-
v8::MaybeLocal<v8::Object> object(t->NewInstance());
32-
return Object::Maybe(isolate, ObjectTemplate(self)->NewInstance(context));
34+
35+
return Object::Maybe(isolate, t->NewInstance(context));
36+
}
37+
38+
static VALUE SetInternalFieldCount(VALUE self, VALUE value) {
39+
ObjectTemplate t(self);
40+
Locker lock(t);
41+
42+
t->SetInternalFieldCount(NUM2INT(value));
43+
44+
return Qnil;
3345
}
3446
};
3547
}

ext/v8/object.cc

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ namespace rr {
2121
defineMethod("GetPrototype", &GetPrototype).
2222
defineMethod("SetPrototype", &SetPrototype).
2323
defineMethod("ObjectProtoToString", &ObjectProtoToString).
24+
defineMethod("GetConstructorName", &GetConstructorName).
25+
defineMethod("InternalFieldCount", &InternalFieldCount).
26+
defineMethod("GetInternalField", &GetInternalField).
27+
defineMethod("SetInternalField", &SetInternalField).
28+
defineMethod("HasOwnProperty", &HasOwnProperty).
29+
defineMethod("HasRealNamedProperty", &HasRealNamedProperty).
30+
defineMethod("HasRealIndexedProperty", &HasRealIndexedProperty).
31+
defineMethod("HasRealNamedCallbackProperty", &HasRealNamedCallbackProperty).
32+
defineMethod("GetRealNamedPropertyInPrototypeChain", &GetRealNamedPropertyInPrototypeChain).
33+
defineMethod("GetRealNamedProperty", &GetRealNamedProperty).
34+
defineMethod("GetRealNamedPropertyAttributes", &GetRealNamedPropertyAttributes).
35+
defineMethod("GetRealNamedPropertyAttributesInPrototypeChain", &GetRealNamedPropertyAttributesInPrototypeChain).
36+
defineMethod("HasNamedLookupInterceptor", &HasNamedLookupInterceptor).
37+
defineMethod("HasIndexedLookupInterceptor", &HasIndexedLookupInterceptor).
38+
defineMethod("SetHiddenValue", &SetHiddenValue).
39+
defineMethod("GetHiddenValue", &GetHiddenValue).
40+
defineMethod("DeleteHiddenValue", &DeleteHiddenValue).
41+
defineMethod("Clone", &Clone).
42+
defineMethod("CreationContext", &CreationContext).
43+
defineMethod("IsCallable", &IsCallable).
44+
defineMethod("CallAsFunction", &CallAsFunction).
45+
defineMethod("CallAsConstructor", &CallAsConstructor).
2446

2547
store(&Class);
2648
}
@@ -210,6 +232,192 @@ namespace rr {
210232
return String::Maybe(object.getIsolate(), object->ObjectProtoToString(Context(r_context)));
211233
}
212234

235+
VALUE Object::GetConstructorName(VALUE self) {
236+
Object object(self);
237+
Locker lock(object);
238+
239+
return String(object.getIsolate(), object->GetConstructorName());
240+
}
241+
242+
VALUE Object::InternalFieldCount(VALUE self) {
243+
Object object(self);
244+
Locker lock(object);
245+
246+
return INT2FIX(object->InternalFieldCount());
247+
}
248+
249+
VALUE Object::GetInternalField(VALUE self, VALUE index) {
250+
Object object(self);
251+
Locker lock(object);
252+
253+
return Value(object.getIsolate(), object->GetInternalField(NUM2INT(index)));
254+
}
255+
256+
VALUE Object::SetInternalField(VALUE self, VALUE index, VALUE value) {
257+
Object object(self);
258+
Locker lock(object);
259+
260+
object->SetInternalField(NUM2INT(index), *Value(value));
261+
262+
return Qnil;
263+
}
264+
265+
VALUE Object::HasOwnProperty(VALUE self, VALUE r_context, VALUE key) {
266+
Object object(self);
267+
Locker lock(object);
268+
269+
return Bool::Maybe(object->HasOwnProperty(Context(r_context), *Name(key)));
270+
}
271+
272+
VALUE Object::HasRealNamedProperty(VALUE self, VALUE r_context, VALUE key) {
273+
Object object(self);
274+
Locker lock(object);
275+
276+
return Bool::Maybe(object->HasRealNamedProperty(Context(r_context), *Name(key)));
277+
}
278+
279+
VALUE Object::HasRealIndexedProperty(VALUE self, VALUE r_context, VALUE index) {
280+
Object object(self);
281+
Locker lock(object);
282+
283+
return Bool::Maybe(object->HasRealIndexedProperty(Context(r_context), NUM2INT(index)));
284+
}
285+
286+
VALUE Object::HasRealNamedCallbackProperty(VALUE self, VALUE r_context, VALUE key) {
287+
Object object(self);
288+
Locker lock(object);
289+
290+
return Bool::Maybe(object->HasRealNamedCallbackProperty(Context(r_context), *Name(key)));
291+
}
292+
293+
VALUE Object::GetRealNamedPropertyInPrototypeChain(VALUE self, VALUE r_context, VALUE key) {
294+
Object object(self);
295+
Locker lock(object);
296+
297+
return Value::Maybe(object.getIsolate(), object->GetRealNamedPropertyInPrototypeChain(
298+
Context(r_context),
299+
*Name(key)
300+
));
301+
}
302+
303+
VALUE Object::GetRealNamedProperty(VALUE self, VALUE r_context, VALUE key) {
304+
Object object(self);
305+
Locker lock(object);
306+
307+
return Value::Maybe(object.getIsolate(), object->GetRealNamedProperty(
308+
Context(r_context),
309+
*Name(key)
310+
));
311+
}
312+
313+
VALUE Object::GetRealNamedPropertyAttributes(VALUE self, VALUE r_context, VALUE key) {
314+
Object object(self);
315+
Locker lock(object);
316+
317+
return Enum<v8::PropertyAttribute>::Maybe(object->GetRealNamedPropertyAttributes(
318+
Context(r_context),
319+
*Name(key)
320+
));
321+
}
322+
323+
VALUE Object::GetRealNamedPropertyAttributesInPrototypeChain(VALUE self, VALUE r_context, VALUE key) {
324+
Object object(self);
325+
Locker lock(object);
326+
327+
return Enum<v8::PropertyAttribute>::Maybe(object->GetRealNamedPropertyAttributesInPrototypeChain(
328+
Context(r_context),
329+
*Name(key)
330+
));
331+
}
332+
333+
VALUE Object::HasNamedLookupInterceptor(VALUE self) {
334+
Object object(self);
335+
Locker lock(object);
336+
337+
return Bool(object->HasNamedLookupInterceptor());
338+
}
339+
340+
VALUE Object::HasIndexedLookupInterceptor(VALUE self) {
341+
Object object(self);
342+
Locker lock(object);
343+
344+
return Bool(object->HasIndexedLookupInterceptor());
345+
}
346+
347+
VALUE Object::SetHiddenValue(VALUE self, VALUE key, VALUE value) {
348+
Object object(self);
349+
Locker lock(object);
350+
351+
return Bool(object->SetHiddenValue(String(key), Value(value)));
352+
}
353+
354+
VALUE Object::GetHiddenValue(VALUE self, VALUE key) {
355+
Object object(self);
356+
Locker lock(object);
357+
358+
return Value(object.getIsolate(), object->GetHiddenValue(String(key)));
359+
}
360+
361+
VALUE Object::DeleteHiddenValue(VALUE self, VALUE key) {
362+
Object object(self);
363+
Locker lock(object);
364+
365+
return Bool(object->DeleteHiddenValue(String(key)));
366+
}
367+
368+
VALUE Object::Clone(VALUE self) {
369+
Object object(self);
370+
Locker lock(object);
371+
372+
return Object(object.getIsolate(), object->Clone());
373+
}
374+
375+
VALUE Object::CreationContext(VALUE self) {
376+
Object object(self);
377+
Locker lock(object);
378+
379+
return Context(object->CreationContext());
380+
}
381+
382+
VALUE Object::IsCallable(VALUE self) {
383+
Object object(self);
384+
Locker lock(object);
385+
386+
return Bool(object->IsCallable());
387+
}
388+
389+
VALUE Object::CallAsFunction(int argc, VALUE* argv, VALUE self) {
390+
VALUE r_context, recv, r_argv;
391+
rb_scan_args(argc, argv, "30", &r_context, &recv, &r_argv);
392+
393+
Object object(self);
394+
v8::Isolate* isolate = object.getIsolate();
395+
Locker lock(isolate);
396+
397+
std::vector< v8::Handle<v8::Value> > vector(Value::convertRubyArray(isolate, r_argv));
398+
399+
return Value::Maybe(isolate, object->CallAsFunction(
400+
Context(r_context),
401+
Value(recv),
402+
RARRAY_LENINT(r_argv),
403+
&vector[0]
404+
));
405+
}
406+
407+
VALUE Object::CallAsConstructor(VALUE self, VALUE r_context, VALUE argv) {
408+
Object object(self);
409+
v8::Isolate* isolate = object.getIsolate();
410+
Locker lock(isolate);
411+
412+
std::vector< v8::Handle<v8::Value> > vector(Value::convertRubyArray(isolate, argv));
413+
414+
return Value::Maybe(isolate, object->CallAsConstructor(
415+
Context(r_context),
416+
RARRAY_LENINT(argv),
417+
&vector[0]
418+
));
419+
}
420+
213421
Object::operator VALUE() {
214422
Isolate isolate(getIsolate());
215423
Locker lock(isolate);

ext/v8/object.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,35 @@ namespace rr {
2727
static VALUE SetPrototype(VALUE self, VALUE r_context, VALUE prototype);
2828

2929
static VALUE ObjectProtoToString(VALUE self, VALUE r_context);
30+
static VALUE GetConstructorName(VALUE self);
31+
32+
static VALUE InternalFieldCount(VALUE self);
33+
static VALUE GetInternalField(VALUE self, VALUE index);
34+
static VALUE SetInternalField(VALUE self, VALUE index, VALUE value);
35+
36+
static VALUE HasOwnProperty(VALUE self, VALUE r_context, VALUE key);
37+
static VALUE HasRealNamedProperty(VALUE self, VALUE r_context, VALUE key);
38+
static VALUE HasRealIndexedProperty(VALUE self, VALUE r_context, VALUE index);
39+
static VALUE HasRealNamedCallbackProperty(VALUE self, VALUE r_context, VALUE key);
40+
static VALUE GetRealNamedPropertyInPrototypeChain(VALUE self, VALUE r_context, VALUE key);
41+
static VALUE GetRealNamedProperty(VALUE self, VALUE r_context, VALUE key);
42+
static VALUE GetRealNamedPropertyAttributes(VALUE self, VALUE r_context, VALUE key);
43+
static VALUE GetRealNamedPropertyAttributesInPrototypeChain(VALUE self, VALUE r_context, VALUE key);
44+
45+
static VALUE HasNamedLookupInterceptor(VALUE self);
46+
static VALUE HasIndexedLookupInterceptor(VALUE self);
47+
48+
static VALUE SetHiddenValue(VALUE self, VALUE key, VALUE value);
49+
static VALUE GetHiddenValue(VALUE self, VALUE key);
50+
static VALUE DeleteHiddenValue(VALUE self, VALUE key);
51+
52+
static VALUE Clone(VALUE self);
53+
static VALUE CreationContext(VALUE self);
54+
55+
static VALUE IsCallable(VALUE self);
56+
57+
static VALUE CallAsFunction(int argc, VALUE* argv, VALUE self);
58+
static VALUE CallAsConstructor(VALUE self, VALUE r_context, VALUE argv);
3059

3160
inline Object(VALUE value) : Ref<v8::Object>(value) {}
3261
inline Object(v8::Isolate* isolate, v8::Handle<v8::Object> object) : Ref<v8::Object>(isolate, object) {}

0 commit comments

Comments
 (0)