Permalink
Browse files

Change to use v8-typed-array.

  • Loading branch information...
1 parent 8780802 commit 59f94998d33cf57876baf3e8f108e09077f14184 @puffnfresh committed May 29, 2011
Showing with 10 additions and 136 deletions.
  1. +1 −0 README.md
  2. +2 −2 examples/example-include.js
  3. +7 −7 src/glcontext.h
  4. +0 −125 src/typedarray.h
  5. +0 −2 src/webgl.cc
View
@@ -18,6 +18,7 @@ Building
--------
node-waf configure build
+ npm install https://github.com/pufuwozu/v8-typed-array/tarball/master
Testing
-------
@@ -5,8 +5,8 @@ function init(canvas, nodejs) {
if(nodejs) {
var webgl = require('../webgl');
- ArrayBuffer = require('buffer').Buffer;
- Float32Array = webgl.Float32Array;
+ ArrayBuffer = require('typed-array').ArrayBuffer;
+ Float32Array = require('typed-array').Float32Array;
Image = webgl.Image;
}
View
@@ -5,7 +5,6 @@
#include <node.h>
#include <node_events.h>
-#include "typedarray.h"
#include "image.h"
using namespace v8;
@@ -185,11 +184,12 @@ class GLContext : public ObjectWrap {
BufferData (const Arguments& args) {
HandleScope scope;
- Float32Array *fa = Unwrap<Float32Array>(args[1]->ToObject());
+ Local<Object> array = args[1]->ToObject();
+ Local<Object> buffer = array->GetHiddenValue(String::New("_buffer"))->ToObject();
GLenum target = args[0]->Uint32Value();
- GLsizei size = fa->Length();
- const GLvoid* data = fa->GetData();
+ GLsizei size = buffer->Get(String::New("byteLength"))->Uint32Value();
+ const GLvoid* data = array->GetIndexedPropertiesExternalArrayData();
GLenum usage = args[2]->Uint32Value();
glBufferData(target, size, data, usage);
@@ -274,12 +274,12 @@ class GLContext : public ObjectWrap {
UniformMatrix4fv (const Arguments &args) {
HandleScope scope;
- Float32Array *fa = Unwrap<Float32Array>(args[2]->ToObject());
+ Local<Object> array = args[2]->ToObject();
GLint location = args[0]->IntegerValue();
- GLsizei count = fa->Length() / sizeof(float);
+ GLsizei count = array->GetIndexedPropertiesExternalArrayDataLength();
GLboolean transpose = args[1]->BooleanValue();
- const GLfloat *value = (float *)fa->GetData();
+ const GLfloat *value = (float *)array->GetIndexedPropertiesExternalArrayData();
glUniformMatrix4fv(location, count, transpose, value);
return Undefined();
View
@@ -1,125 +0,0 @@
-#ifndef TYPEDARRAYS_H_
-#define TYPEDARRAYS_H_
-
-#include <v8.h>
-
-#include <node.h>
-#include <node_buffer.h>
-
-using namespace v8;
-using namespace node;
-
-#define SET_
-
-class Float32Array : public ObjectWrap {
-public:
-
- static void
- Initialize (Handle<Object> target) {
- HandleScope scope;
-
- Local<FunctionTemplate> t = FunctionTemplate::New(New);
- t->InstanceTemplate()->SetInternalFieldCount(1);
- t->SetClassName(String::NewSymbol("Float32Array"));
-
- SetBytesPerElement(t, 4);
-
- NODE_SET_PROTOTYPE_METHOD(t, "set", Set);
-
- t->PrototypeTemplate()->SetAccessor(String::New("length"), LengthGetter);
-
- target->Set(String::NewSymbol("Float32Array"), t->GetFunction());
- }
-
- void *
- GetData () {
- Local<Value> buf = handle_->GetHiddenValue(String::New("buffer"));
-
- return (void *)Buffer::Data(buf->ToObject());
- }
-
- size_t
- Length () {
- Local<Value> buf = handle_->GetHiddenValue(String::New("buffer"));
-
- return Buffer::Length(buf->ToObject());
- }
-
- void
- Set (float *sequence, uint32_t length) {
- Local<Value> buf = handle_->GetHiddenValue(String::New("buffer"));
-
- float *data = (float *)Buffer::Data(buf->ToObject());
- for(int i = 0; i < length; i++) {
- data[i] = sequence[i];
- }
- }
-
-protected:
-
- static Handle<Value>
- New (const Arguments& args) {
- HandleScope scope;
-
- Float32Array *fa = new Float32Array();
- fa->Wrap(args.This());
-
- 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();
- }
-
- static Handle<Value>
- Set (const Arguments& args) {
- HandleScope scope;
-
- Float32Array *fa = Unwrap<Float32Array>(args.This());
-
- Local<Array> a = Local<Array>::Cast(args[0]);
-
- size_t length = a->Length();
- float *sequence = new float[length];
- for (int i = 0; i < length; i++) {
- Local<Value> val = a->Get(i);
- sequence[i] = val->NumberValue();
- }
- fa->Set(sequence, length);
- delete[] sequence;
-
- return Undefined();
- }
-
- static Handle<Value>
- LengthGetter (Local<String> property, const AccessorInfo& info) {
- HandleScope scope;
-
- Float32Array *fa = Unwrap<Float32Array>(info.This());
- Local<Value> buf = fa->handle_->GetHiddenValue(String::New("buffer"));
-
- return scope.Close(Integer::New(Buffer::Length(buf->ToObject())));
- }
-
- Float32Array () : ObjectWrap() {
- }
-
-private:
-
- static void
- SetBytesPerElement (Handle<FunctionTemplate> function, unsigned long value) {
- function->Set(String::New("BYTES_PER_ELEMENT"), Integer::New(value), ReadOnly);
- }
-
-};
-
-#endif // TYPEDARRAYS_H_
View
@@ -6,7 +6,6 @@
#include <node_events.h>
#include "glcontext.h"
-#include "typedarray.h"
#include "image.h"
using namespace v8;
@@ -190,7 +189,6 @@ init (Handle<Object> target) {
HandleScope scope;
GLContext::Initialize(target);
- Float32Array::Initialize(target);
Image::Initialize(target);
Window::Initialize(target);
}

0 comments on commit 59f9499

Please sign in to comment.