2010-05-13 20:43:46 -04:00
|
|
|
#include "v8_array.h"
|
2010-05-17 11:30:31 -04:00
|
|
|
#include "v8_ref.h"
|
|
|
|
#include "v8_obj.h"
|
2010-05-13 20:43:46 -04:00
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2010-05-17 11:30:31 -04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
VALUE ArrayClass;
|
|
|
|
|
|
|
|
Local<Array> unwrap(VALUE self) {
|
|
|
|
return V8_Ref_Get<Array>(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE New(int argc, VALUE *argv, VALUE self) {
|
2010-05-22 02:16:30 -04:00
|
|
|
if (!Context::InContext()) {
|
|
|
|
rb_raise(rb_eScriptError, "must be in a context to call Array::New()");
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-05-17 11:30:31 -04:00
|
|
|
VALUE length;
|
2010-05-22 02:16:30 -04:00
|
|
|
rb_scan_args(argc, argv, "01", &length);
|
2010-05-17 11:30:31 -04:00
|
|
|
if (NIL_P(length)) {
|
|
|
|
length = INT2FIX(0);
|
|
|
|
}
|
2010-05-22 22:31:36 -04:00
|
|
|
HandleScope scope;
|
2010-06-07 03:50:33 -04:00
|
|
|
return rr_v8_ref_create(self, Array::New(NUM2INT(length)));
|
2010-05-17 11:30:31 -04:00
|
|
|
}
|
2010-05-13 20:43:46 -04:00
|
|
|
|
2010-05-17 11:30:31 -04:00
|
|
|
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_cV8_C_Object);
|
|
|
|
rr_define_singleton_method(ArrayClass, "New", New, -1);
|
|
|
|
rr_define_method(ArrayClass, "Length", Length, 0);
|
|
|
|
rr_define_method(ArrayClass, "CloneElementAt", CloneElementAt, 1);
|
2010-05-13 20:43:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE rr_reflect_v8_array(Handle<Value> value) {
|
|
|
|
Local<Array> array(Array::Cast(*value));
|
2010-06-08 12:51:09 -04:00
|
|
|
Local<Value> peer = array->GetHiddenValue(String::NewSymbol("TheRubyRacer::RubyObject"));
|
2010-06-07 03:13:26 -04:00
|
|
|
return peer.IsEmpty() ? rr_v8_ref_create(ArrayClass, value) : (VALUE)External::Unwrap(peer);
|
2010-05-13 20:43:46 -04:00
|
|
|
}
|