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

(almost) fully wrap the object interface

This commit is contained in:
Charles Lowell 2012-05-17 10:30:37 -05:00
parent 72e72ada55
commit 77148914ab
7 changed files with 303 additions and 190 deletions

10
ext/v8/array.cc Normal file
View file

@ -0,0 +1,10 @@
#include "rr.h"
namespace rr {
void Array::Init() {
ClassBuilder("Array", Object::Class).
store(&Class);
}
}

View file

@ -16,7 +16,9 @@ extern "C" {
Value::Init();
String::Init();
Object::Init();
Array::Init();
External::Init();
Script::Init();
Template::Init();
}
}

View file

@ -3,7 +3,7 @@
namespace rr {
void Object::Init() {
ClassBuilder("Object", "Value").
ClassBuilder("Object", Value::Class).
defineSingletonMethod("New", &New).
defineMethod("Set", &Set).
defineMethod("ForceSet", &ForceSet).
@ -13,6 +13,44 @@ void Object::Init() {
defineMethod("Delete", &Delete).
defineMethod("ForceDelete", &ForceDelete).
defineMethod("SetAccessor", &SetAccessor).
defineMethod("GetPropertyNames", &GetPropertyNames).
defineMethod("GetOwnPropertyNames", &GetOwnPropertyNames).
defineMethod("GetPrototype", &GetPrototype).
defineMethod("SetPrototype", &SetPrototype).
defineMethod("FindInstanceInPrototypeChain", &FindInstanceInPrototypeChain).
defineMethod("ObjectProtoToString", &ObjectProtoToString).
defineMethod("GetConstructorName", &GetConstructorName).
defineMethod("InternalFieldCount", &InternalFieldCount).
defineMethod("GetInternalField", &GetInternalField).
defineMethod("SetInternalField", &SetInternalField).
defineMethod("HasOwnProperty", &HasOwnProperty).
defineMethod("HasRealNamedProperty", &HasRealNamedProperty).
defineMethod("HasRealIndexedProperty", &HasRealIndexedProperty).
defineMethod("HasRealNamedCallbackProperty", &HasRealNamedCallbackProperty).
defineMethod("GetRealNamedPropertyInPrototypeChain", &GetRealNamedPropertyInPrototypeChain).
defineMethod("GetRealNamedProperty", &GetRealNamedProperty).
defineMethod("HasNamedLookupInterceptor", &HasNamedLookupInterceptor).
defineMethod("HasIndexedLookupInterceptor", &HasIndexedLookupInterceptor).
defineMethod("TurnOnAccessCheck", &TurnOnAccessCheck).
defineMethod("GetIdentityHash", &GetIdentityHash).
defineMethod("SetHiddenValue", &SetHiddenValue).
defineMethod("GetHiddenValue", &GetHiddenValue).
defineMethod("DeleteHiddenValue", &DeleteHiddenValue).
defineMethod("IsDirty", &IsDirty).
defineMethod("Clone", &Clone).
defineMethod("CreationContext", &CreationContext).
defineMethod("SetIndexedPropertiesToPixelData", &SetIndexedPropertiesToPixelData).
defineMethod("GetIndexedPropertiesPixelData", &GetIndexedPropertiesPixelData).
defineMethod("HasIndexedPropertiesToPixelData", &HasIndexedPropertiesInPixelData).
defineMethod("GetIndexedPropertiesPixelDataLength", &GetIndexedPropertiesPixelDataLength).
defineMethod("SetIndexedPropertiesToExternalArrayData", &SetIndexedPropertiesToExternalArrayData).
defineMethod("HasIndexedPropertiesInExternalArrayData", &HasIndexedPropertiesInExternalArrayData).
defineMethod("GetIndexedPropertiesExternalArrayData", &GetIndexedPropertiesExternalArrayData).
defineMethod("GetIndexedPropertiesExternalArrayDataType", &GetIndexedPropertiesExternalArrayDataType).
defineMethod("GetIndexedPropertiesExternalArrayDataLength", &GetIndexedPropertiesExternalArrayDataLength).
defineMethod("IsCallable", &IsCallable).
defineMethod("CallAsFunction", &CallAsFunction).
defineMethod("CallAsConstructor", &CallAsConstructor).
store(&Class);
ClassBuilder("PropertyAttribute").
defineEnumConst("None", v8::None).
@ -61,7 +99,7 @@ VALUE Object::Has(VALUE self, VALUE key) {
if (rb_obj_is_kind_of(key, rb_cNumeric)) {
return Bool(obj->Has(UInt32(key)));
} else {
return Bool(obj->Has(*String(key)));
return Bool(obj->Has(*String(key)));
}
}
@ -91,191 +129,6 @@ VALUE Object::SetAccessor(int argc, VALUE* argv, VALUE self) {
PropertyAttribute(attribs)));
}
//
// /**
// * Returns an array containing the names of the enumerable properties
// * of this object, including properties from prototype objects. The
// * array returned by this method contains the same values as would
// * be enumerated by a for-in statement over this object.
// */
// V8EXPORT Local<Array> GetPropertyNames();
//
// /**
// * This function has the same functionality as GetPropertyNames but
// * the returned array doesn't contain the names of properties from
// * prototype objects.
// */
// V8EXPORT Local<Array> GetOwnPropertyNames();
//
// /**
// * Get the prototype object. This does not skip objects marked to
// * be skipped by __proto__ and it does not consult the security
// * handler.
// */
// V8EXPORT Local<Value> GetPrototype();
//
// /**
// * Set the prototype object. This does not skip objects marked to
// * be skipped by __proto__ and it does not consult the security
// * handler.
// */
// V8EXPORT bool SetPrototype(Handle<Value> prototype);
//
// /**
// * Finds an instance of the given function template in the prototype
// * chain.
// */
// V8EXPORT Local<Object> FindInstanceInPrototypeChain(
// Handle<FunctionTemplate> tmpl);
//
// /**
// * Call builtin Object.prototype.toString on this object.
// * This is different from Value::ToString() that may call
// * user-defined toString function. This one does not.
// */
// V8EXPORT Local<String> ObjectProtoToString();
//
// /**
// * Returns the name of the function invoked as a constructor for this object.
// */
// V8EXPORT Local<String> GetConstructorName();
//
// /** Gets the number of internal fields for this Object. */
// V8EXPORT int InternalFieldCount();
// /** Gets the value in an internal field. */
// inline Local<Value> GetInternalField(int index);
// /** Sets the value in an internal field. */
// V8EXPORT void SetInternalField(int index, Handle<Value> value);
//
// /** Gets a native pointer from an internal field. */
// inline void* GetPointerFromInternalField(int index);
//
// /** Sets a native pointer in an internal field. */
// V8EXPORT void SetPointerInInternalField(int index, void* value);
//
// // Testers for local properties.
// V8EXPORT bool HasOwnProperty(Handle<String> key);
// V8EXPORT bool HasRealNamedProperty(Handle<String> key);
// V8EXPORT bool HasRealIndexedProperty(uint32_t index);
// V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
//
// /**
// * If result.IsEmpty() no real property was located in the prototype chain.
// * This means interceptors in the prototype chain are not called.
// */
// V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
// Handle<String> key);
//
// /**
// * If result.IsEmpty() no real property was located on the object or
// * in the prototype chain.
// * This means interceptors in the prototype chain are not called.
// */
// V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
//
// /** Tests for a named lookup interceptor.*/
// V8EXPORT bool HasNamedLookupInterceptor();
//
// /** Tests for an index lookup interceptor.*/
// V8EXPORT bool HasIndexedLookupInterceptor();
//
// /**
// * Turns on access check on the object if the object is an instance of
// * a template that has access check callbacks. If an object has no
// * access check info, the object cannot be accessed by anyone.
// */
// V8EXPORT void TurnOnAccessCheck();
//
// /**
// * Returns the identity hash for this object. The current implementation
// * uses a hidden property on the object to store the identity hash.
// *
// * The return value will never be 0. Also, it is not guaranteed to be
// * unique.
// */
// V8EXPORT int GetIdentityHash();
//
// /**
// * Access hidden properties on JavaScript objects. These properties are
// * hidden from the executing JavaScript and only accessible through the V8
// * C++ API. Hidden properties introduced by V8 internally (for example the
// * identity hash) are prefixed with "v8::".
// */
// V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
// V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
// V8EXPORT bool DeleteHiddenValue(Handle<String> key);
//
// /**
// * Returns true if this is an instance of an api function (one
// * created from a function created from a function template) and has
// * been modified since it was created. Note that this method is
// * conservative and may return true for objects that haven't actually
// * been modified.
// */
// V8EXPORT bool IsDirty();
//
// /**
// * Clone this object with a fast but shallow copy. Values will point
// * to the same values as the original object.
// */
// V8EXPORT Local<Object> Clone();
//
// /**
// * Returns the context in which the object was created.
// */
// V8EXPORT Local<Context> CreationContext();
//
// /**
// * Set the backing store of the indexed properties to be managed by the
// * embedding layer. Access to the indexed properties will follow the rules
// * spelled out in CanvasPixelArray.
// * Note: The embedding program still owns the data and needs to ensure that
// * the backing store is preserved while V8 has a reference.
// */
// V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
// V8EXPORT bool HasIndexedPropertiesInPixelData();
// V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
// V8EXPORT int GetIndexedPropertiesPixelDataLength();
//
// /**
// * Set the backing store of the indexed properties to be managed by the
// * embedding layer. Access to the indexed properties will follow the rules
// * spelled out for the CanvasArray subtypes in the WebGL specification.
// * Note: The embedding program still owns the data and needs to ensure that
// * the backing store is preserved while V8 has a reference.
// */
// V8EXPORT void SetIndexedPropertiesToExternalArrayData(
// void* data,
// ExternalArrayType array_type,
// int number_of_elements);
// V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
// V8EXPORT void* GetIndexedPropertiesExternalArrayData();
// V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
// V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
//
// /**
// * Checks whether a callback is set by the
// * ObjectTemplate::SetCallAsFunctionHandler method.
// * When an Object is callable this method returns true.
// */
// V8EXPORT bool IsCallable();
//
// /**
// * Call an Object as a function if a callback is set by the
// * ObjectTemplate::SetCallAsFunctionHandler method.
// */
// V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
// int argc,
// Handle<Value> argv[]);
//
// /**
// * Call an Object as a constructor if a callback is set by the
// * ObjectTemplate::SetCallAsFunctionHandler method.
// * Note: This method behaves like the Function::NewInstance method.
// */
// V8EXPORT Local<Value> CallAsConstructor(int argc,
// Handle<Value> argv[]);
Object::operator VALUE() {
if (handle->IsFunction()) {
// return Function(handle);
@ -300,4 +153,159 @@ Object::operator VALUE() {
}
return Ref<v8::Object>::operator VALUE();
}
VALUE Object::GetPropertyNames(VALUE self) {
return Array(Object(self)->GetPropertyNames());
}
VALUE Object::GetOwnPropertyNames(VALUE self) {
return Array(Object(self)->GetOwnPropertyNames());
}
VALUE Object::GetPrototype(VALUE self) {
return Value(Object(self)->GetPrototype());
}
VALUE Object::SetPrototype(VALUE self, VALUE prototype) {
return Bool(Object(self)->SetPrototype(Value(prototype)));
}
VALUE Object::FindInstanceInPrototypeChain(VALUE self, VALUE impl) {
return Object(Object(self)->FindInstanceInPrototypeChain(FunctionTemplate(impl)));
}
VALUE Object::ObjectProtoToString(VALUE self) {
return String(Object(self)->ObjectProtoToString());
}
VALUE Object::GetConstructorName(VALUE self) {
return String(Object(self)->GetConstructorName());
}
VALUE Object::InternalFieldCount(VALUE self) {
return Int(Object(self)->InternalFieldCount());
}
VALUE Object::GetInternalField(VALUE self, VALUE idx) {
return Value(Object(self)->GetInternalField(Int(idx)));
}
VALUE Object::SetInternalField(VALUE self, VALUE idx, VALUE value) {
Void(Object(self)->SetInternalField(Int(idx), Value(value)));
}
VALUE Object::HasOwnProperty(VALUE self, VALUE key) {
return Bool(Object(self)->HasOwnProperty(String(key)));
}
VALUE Object::HasRealNamedProperty(VALUE self, VALUE key) {
return Bool(Object(self)->HasRealNamedProperty(String(key)));
}
VALUE Object::HasRealIndexedProperty(VALUE self, VALUE idx) {
return Bool(Object(self)->HasRealIndexedProperty(UInt32(idx)));
}
VALUE Object::HasRealNamedCallbackProperty(VALUE self, VALUE key) {
return Bool(Object(self)->HasRealNamedCallbackProperty(String(key)));
}
VALUE Object::GetRealNamedPropertyInPrototypeChain(VALUE self, VALUE key) {
return Value(Object(self)->GetRealNamedPropertyInPrototypeChain(String(key)));
}
VALUE Object::GetRealNamedProperty(VALUE self, VALUE key) {
return Value(Object(self)->GetRealNamedProperty(String(key)));
}
VALUE Object::HasNamedLookupInterceptor(VALUE self) {
return Bool(Object(self)->HasNamedLookupInterceptor());
}
VALUE Object::HasIndexedLookupInterceptor(VALUE self) {
return Bool(Object(self)->HasIndexedLookupInterceptor());
}
VALUE Object::TurnOnAccessCheck(VALUE self) {
Void(Object(self)->TurnOnAccessCheck());
}
VALUE Object::GetIdentityHash(VALUE self) {
return Int(Object(self)->GetIdentityHash());
}
VALUE Object::SetHiddenValue(VALUE self, VALUE key, VALUE value) {
return Bool(Object(self)->SetHiddenValue(String(key), Value(value)));
}
VALUE Object::GetHiddenValue(VALUE self, VALUE key) {
return Value(Object(self)->GetHiddenValue(String(key)));
}
VALUE Object::DeleteHiddenValue(VALUE self, VALUE key) {
return Bool(Object(self)->DeleteHiddenValue(String(key)));
}
VALUE Object::IsDirty(VALUE self) {
return Bool(Object(self)->IsDirty());
}
VALUE Object::Clone(VALUE self) {
return Object(Object(self)->Clone());
}
VALUE Object::CreationContext(VALUE self) {
return Context(Object(self)->CreationContext());
}
VALUE Object::SetIndexedPropertiesToPixelData(VALUE self, VALUE data, VALUE length) {
return not_implemented("SetIndexedPropertiesToPixelData");
}
VALUE Object::GetIndexedPropertiesPixelData(VALUE self) {
return not_implemented("GetIndexedPropertiesPixelData");
}
VALUE Object::HasIndexedPropertiesInPixelData(VALUE self) {
return Bool(Object(self)->HasIndexedPropertiesInPixelData());
}
VALUE Object::GetIndexedPropertiesPixelDataLength(VALUE self) {
return Int(Object(self)->GetIndexedPropertiesPixelDataLength());
}
VALUE Object::SetIndexedPropertiesToExternalArrayData(VALUE self) {
return not_implemented("SetIndexedPropertiesToExternalArrayData");
}
VALUE Object::HasIndexedPropertiesInExternalArrayData(VALUE self) {
return Bool(Object(self)->HasIndexedPropertiesInExternalArrayData());
}
VALUE Object::GetIndexedPropertiesExternalArrayData(VALUE self) {
return not_implemented("GetIndexedPropertiesExternalArrayData");
}
VALUE Object::GetIndexedPropertiesExternalArrayDataType(VALUE self) {
return not_implemented("GetIndexedPropertiesExternalArrayDataType");
}
VALUE Object::GetIndexedPropertiesExternalArrayDataLength(VALUE self) {
return Int(Object(self)->GetIndexedPropertiesExternalArrayDataLength());
}
VALUE Object::IsCallable(VALUE self) {
return Bool(Object(self)->IsCallable());
}
VALUE Object::CallAsFunction(VALUE self, VALUE recv, VALUE argc, VALUE argv) {
std::vector< v8::Handle<v8::Value> > v(NUM2INT(argc));
return Value(Object(self)->CallAsFunction(Object(recv), Int(argc), Value::array(argv, v)));
}
VALUE Object::CallAsConstructor(VALUE self, VALUE argc, VALUE argv) {
std::vector< v8::Handle<v8::Value> > v(NUM2INT(argc));
return Value(Object(self)->CallAsConstructor(Int(argc), Value::array(argv, v)));
}
}

View file

@ -15,6 +15,10 @@ namespace rr {
return rb_define_module_under(V8_C, name);
}
VALUE not_implemented(const char* message) {
Void(rb_raise(rb_eStandardError, "not yet implemented %s", message));
}
ClassBuilder::ClassBuilder(const char* name, VALUE superclass) {
this->value = defineClass(name, superclass);
}
@ -39,6 +43,10 @@ namespace rr {
rb_define_method(this->value, name, (VALUE (*)(...))impl, 2);
return *this;
}
ClassBuilder& ClassBuilder::defineMethod(const char* name, VALUE (*impl)(VALUE, VALUE, VALUE, VALUE)) {
rb_define_method(this->value, name, (VALUE (*)(...))impl, 3);
return *this;
}
ClassBuilder& ClassBuilder::defineSingletonMethod(const char* name, VALUE (*impl)(int, VALUE*, VALUE)) {
rb_define_singleton_method(this->value, name, (VALUE (*)(...))impl, -1);
return *this;
@ -55,6 +63,10 @@ namespace rr {
rb_define_singleton_method(this->value, name, (VALUE (*)(...))impl, 2);
return *this;
}
ClassBuilder& ClassBuilder::defineSingletonMethod(const char* name, VALUE (*impl)(VALUE, VALUE, VALUE, VALUE)) {
rb_define_singleton_method(this->value, name, (VALUE (*)(...))impl, 3);
return *this;
}
ClassBuilder& ClassBuilder::defineEnumConst(const char* name, int value) {
rb_define_const(this->value, name, INT2FIX(value));
return *this;

View file

@ -3,13 +3,17 @@
#include <v8.h>
#include <ruby.h>
#include <vector>
namespace rr {
VALUE Bool(bool b);
uint32_t UInt32(VALUE num);
#define Void(expr) expr; return Qnil;
VALUE Bool(bool b);
int Int(VALUE v);
uint32_t UInt32(VALUE num);
VALUE not_implemented(const char* message);
class GC {
public:
class Queue {
@ -73,6 +77,13 @@ public:
inline v8::Handle<T> GetHandle() const { return *this;}
inline v8::Handle<T> operator*() const {return *this;}
static v8::Handle<T> * array(VALUE argv, std::vector< v8::Handle<T> >& v) {
for (uint32_t i = 0; i < v.size(); i++) {
v[i] = Ref<T>(rb_ary_entry(argv, i));
}
return &v[0];
}
class Holder {
friend class Ref;
public:
@ -257,12 +268,67 @@ public:
static VALUE Delete(VALUE self, VALUE key);
static VALUE ForceDelete(VALUE self, VALUE key);
static VALUE SetAccessor(int argc, VALUE* argv, VALUE self);
static VALUE GetPropertyNames(VALUE self);
static VALUE GetOwnPropertyNames(VALUE self);
static VALUE GetPrototype(VALUE self);
static VALUE SetPrototype(VALUE self, VALUE prototype);
static VALUE FindInstanceInPrototypeChain(VALUE self, VALUE impl);
static VALUE ObjectProtoToString(VALUE self);
static VALUE GetConstructorName(VALUE self);
static VALUE InternalFieldCount(VALUE self);
static VALUE GetInternalField(VALUE self, VALUE idx);
static VALUE SetInternalField(VALUE self, VALUE idx, VALUE value);
static VALUE HasOwnProperty(VALUE self, VALUE key);
static VALUE HasRealNamedProperty(VALUE self, VALUE key);
static VALUE HasRealIndexedProperty(VALUE self, VALUE idx);
static VALUE HasRealNamedCallbackProperty(VALUE self, VALUE key);
static VALUE GetRealNamedPropertyInPrototypeChain(VALUE self, VALUE key);
static VALUE GetRealNamedProperty(VALUE self, VALUE key);
static VALUE HasNamedLookupInterceptor(VALUE self);
static VALUE HasIndexedLookupInterceptor(VALUE self);
static VALUE TurnOnAccessCheck(VALUE self);
static VALUE GetIdentityHash(VALUE self);
static VALUE SetHiddenValue(VALUE self, VALUE key, VALUE value);
static VALUE GetHiddenValue(VALUE self, VALUE key);
static VALUE DeleteHiddenValue(VALUE self, VALUE key);
static VALUE IsDirty(VALUE self);
static VALUE Clone(VALUE self);
static VALUE CreationContext(VALUE self);
static VALUE SetIndexedPropertiesToPixelData(VALUE self, VALUE data, VALUE length);
static VALUE GetIndexedPropertiesPixelData(VALUE self);
static VALUE HasIndexedPropertiesInPixelData(VALUE self);
static VALUE GetIndexedPropertiesPixelDataLength(VALUE self);
static VALUE SetIndexedPropertiesToExternalArrayData(VALUE self);
static VALUE HasIndexedPropertiesInExternalArrayData(VALUE self);
static VALUE GetIndexedPropertiesExternalArrayData(VALUE self);
static VALUE GetIndexedPropertiesExternalArrayDataType(VALUE self);
static VALUE GetIndexedPropertiesExternalArrayDataLength(VALUE self);
static VALUE IsCallable(VALUE self);
static VALUE CallAsFunction(VALUE self, VALUE recv, VALUE argc, VALUE argv);
static VALUE CallAsConstructor(VALUE self, VALUE argc, VALUE argv);
inline Object(VALUE value) : Ref<v8::Object>(value) {}
inline Object(v8::Handle<v8::Object> object) : Ref<v8::Object>(object) {}
virtual operator VALUE();
};
class Array : public Ref<v8::Array> {
public:
static void Init();
inline Array(v8::Handle<v8::Array> array) : Ref<v8::Array>(array) {}
};
class Template {
public:
static void Init();
};
class FunctionTemplate : public Ref<v8::FunctionTemplate> {
public:
static void Init();
inline FunctionTemplate(VALUE value) : Ref<v8::FunctionTemplate>(value) {}
};
class V8 {
public:
static void Init();
@ -277,10 +343,12 @@ public:
ClassBuilder& defineMethod(const char* name, VALUE (*impl)(VALUE));
ClassBuilder& defineMethod(const char* name, VALUE (*impl)(VALUE, VALUE));
ClassBuilder& defineMethod(const char* name, VALUE (*impl)(VALUE, VALUE, VALUE));
ClassBuilder& defineMethod(const char* name, VALUE (*impl)(VALUE, VALUE, VALUE, VALUE));
ClassBuilder& defineSingletonMethod(const char* name, VALUE (*impl)(int, VALUE*, VALUE));
ClassBuilder& defineSingletonMethod(const char* name, VALUE (*impl)(VALUE));
ClassBuilder& defineSingletonMethod(const char* name, VALUE (*impl)(VALUE, VALUE));
ClassBuilder& defineSingletonMethod(const char* name, VALUE (*impl)(VALUE, VALUE, VALUE));
ClassBuilder& defineSingletonMethod(const char* name, VALUE (*impl)(VALUE, VALUE, VALUE, VALUE));
ClassBuilder& defineEnumConst(const char* name, int value);
ClassBuilder& store(VALUE* storage);
inline operator VALUE() {return this->value;}

View file

@ -3,7 +3,7 @@
namespace rr {
void String::Init() {
ClassBuilder("String", "Value").
ClassBuilder("String", Value::Class).
defineSingletonMethod("New", &New).
defineSingletonMethod("Concat", &Concat).
defineMethod("Utf8Value", &Utf8Value).

13
ext/v8/template.cc Normal file
View file

@ -0,0 +1,13 @@
#include "rr.h"
namespace rr {
void Template::Init() {
ClassBuilder("Template");
FunctionTemplate::Init();
}
void FunctionTemplate::Init() {
ClassBuilder("FunctionTemplate", "Template").
store(&Class);
}
}