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

expose v8::Array

This commit is contained in:
Charles Lowell 2012-05-17 11:04:05 -05:00
parent 77148914ab
commit 8adcc30e3a
4 changed files with 51 additions and 2 deletions

View file

@ -4,7 +4,23 @@ namespace rr {
void Array::Init() {
ClassBuilder("Array", Object::Class).
defineSingletonMethod("New", &New).
defineMethod("Length", &Length).
defineMethod("CloneElementAt", &CloneElementAt).
store(&Class);
}
VALUE Array::New(int argc, VALUE argv[], VALUE self) {
VALUE length; rb_scan_args(argc, argv, "01", &length);
return Array(v8::Array::New(Int(length)));
}
VALUE Array::Length(VALUE self) {
return UINT2NUM(Array(self)->Length());
}
VALUE Array::CloneElementAt(VALUE self, VALUE index) {
return Object(Array(self)->CloneElementAt(UInt32(index)));
}
}

View file

@ -6,8 +6,8 @@ VALUE Bool(bool b) {
return b ? Qtrue : Qfalse;
}
VALUE Int(int i) {
return INT2FIX(i);
int Int(VALUE v) {
return RTEST(v) ? NUM2INT(v) : 0;
}
uint32_t UInt32(VALUE num) {

View file

@ -315,7 +315,12 @@ public:
class Array : public Ref<v8::Array> {
public:
static void Init();
static VALUE New(int argc, VALUE argv[], VALUE self);
static VALUE Length(VALUE self);
static VALUE CloneElementAt(VALUE self, VALUE index);
inline Array(v8::Handle<v8::Array> array) : Ref<v8::Array>(array) {}
inline Array(VALUE value) : Ref<v8::Array>(value) {}
};
class Template {

28
spec/c/array_spec.rb Normal file
View file

@ -0,0 +1,28 @@
require 'spec_helper'
describe V8::C::Array do
before do
@cxt = V8::C::Context::New()
@cxt.Enter()
end
after do
@cxt.Exit()
end
it "can store and retrieve a value" do
V8::C::HandleScope() do
o = V8::C::Object::New()
a = V8::C::Array::New()
a.Length().should eql 0
a.Set(0, o)
a.Length().should eql 1
a.Get(0).Equals(o).should be_true
end
end
it "can be initialized with a length" do
V8::C::HandleScope() do
a = V8::C::Array::New(5)
a.Length().should eql 5
end
end
end