1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/ext/v8/v8_array.cpp
2011-04-13 08:45:07 -05:00

50 lines
No EOL
1.3 KiB
C++

#include "v8_handle.h"
#include "v8_array.h"
#include "v8_object.h"
using namespace v8;
namespace {
VALUE ArrayClass;
Persistent<Array>& unwrap(VALUE self) {
return rr_v8_handle<Array>(self);
}
VALUE New(int argc, VALUE *argv, VALUE self) {
if (!Context::InContext()) {
rb_raise(rb_eScriptError, "must be in a context to call Array::New()");
return Qnil;
}
VALUE length;
rb_scan_args(argc, argv, "01", &length);
if (NIL_P(length)) {
length = INT2FIX(0);
}
HandleScope scope;
return rr_v8_handle_new(self, Array::New(NUM2INT(length)));
}
VALUE Length(VALUE self) {
return rr_v82rb(unwrap(self)->Length());
}
VALUE CloneElementAt(VALUE self, VALUE index) {
return rr_v82rb(unwrap(self)->CloneElementAt(NUM2UINT(index)));
}
}
void rr_init_v8_array() {
ArrayClass = rr_define_class("Array", rr_v8_object_class());
rr_define_singleton_method(ArrayClass, "New", New, -1);
rr_define_method(ArrayClass, "Length", Length, 0);
rr_define_method(ArrayClass, "CloneElementAt", CloneElementAt, 1);
}
VALUE rr_reflect_v8_array(Handle<Value> value) {
Local<Array> array(Array::Cast(*value));
Local<Value> peer = array->GetHiddenValue(String::NewSymbol("TheRubyRacer::RubyObject"));
return peer.IsEmpty() ? rr_v8_handle_new(ArrayClass, value) : (VALUE)External::Unwrap(peer);
}