Permalink
Browse files

Allow Float32Array creation from Number value

  • Loading branch information...
1 parent d3a7ac8 commit 276235a72a66f02965a9f58a5461f23ee232eb7c @puffnfresh committed May 2, 2011
Showing with 13 additions and 2 deletions.
  1. +13 −2 src/typedarray.h
View
@@ -64,7 +64,18 @@ class Float32Array : public ObjectWrap {
Float32Array *fa = new Float32Array();
fa->Wrap(args.This());
- fa->handle_->SetHiddenValue(String::New("buffer"), args[0]);
+ Local<Value> buffer = args[0];
+ if (buffer->IsNumber()) {
+ Handle<Integer> length = buffer->ToInteger();
+
+ Local<Object> global = Context::GetCurrent()->Global();
+ Local<Value> bv = global->Get(String::NewSymbol("Buffer"));
+ Local<Function> b = Local<Function>::Cast(bv);
+
+ Local<Value> argv[1] = { Local<Value>::New(length) };
+ buffer = b->NewInstance(1, argv);
+ }
+ fa->handle_->SetHiddenValue(String::New("buffer"), buffer);
return args.This();
}
@@ -79,7 +90,7 @@ class Float32Array : public ObjectWrap {
size_t length = a->Length();
float *sequence = new float[length];
- for(int i = 0; i < length; i++) {
+ for (int i = 0; i < length; i++) {
Local<Value> val = a->Get(i);
sequence[i] = val->NumberValue();
}

0 comments on commit 276235a

Please sign in to comment.