|
| 1 | +#include <string> |
| 2 | +#include <node.h> |
| 3 | +#include "ArrayBuffer.h" |
| 4 | + |
| 5 | +using namespace v8; |
| 6 | + |
| 7 | +void StringArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 8 | + Isolate *isolate = args.GetIsolate(); |
| 9 | + |
| 10 | + const char *str = "!!!EHLO WORLD!!!"; |
| 11 | + node::ArrayBuffer *buffer = node::ArrayBuffer::New(isolate, str); |
| 12 | + |
| 13 | + args.GetReturnValue().Set(buffer->ToArrayBuffer()); |
| 14 | +} |
| 15 | + |
| 16 | +void CreateArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 17 | + Isolate *isolate = args.GetIsolate(); |
| 18 | + |
| 19 | + std::string str = "Hello World!"; |
| 20 | + node::ArrayBuffer *buffer = node::ArrayBuffer::New(isolate, str); |
| 21 | + |
| 22 | + args.GetReturnValue().Set(buffer->ToArrayBuffer()); |
| 23 | +} |
| 24 | + |
| 25 | +void PrintWrapped(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 26 | + Isolate *isolate = args.GetIsolate(); |
| 27 | + |
| 28 | + if (args[0]->IsArrayBuffer() || args[0]->IsTypedArray()) { |
| 29 | + Local<ArrayBuffer> arraybuffer; |
| 30 | + |
| 31 | + if (args[0]->IsArrayBuffer()) { |
| 32 | + arraybuffer = Local<ArrayBuffer>::Cast(args[0]); |
| 33 | + } else { |
| 34 | + Local<ArrayBufferView> view = Local<ArrayBufferView>::Cast(args[0]); |
| 35 | + arraybuffer = view->Buffer(); |
| 36 | + } |
| 37 | + |
| 38 | + if (!arraybuffer.IsEmpty()) { |
| 39 | + node::ArrayBuffer *buffer = node::ArrayBuffer::New(isolate, arraybuffer); |
| 40 | + std::string str = buffer->Unwrap<std::string>(); |
| 41 | + |
| 42 | + printf("Wrapped: '%s', %zu\n", str.data(), str.length()); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +void PrintArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 48 | + Isolate *isolate = args.GetIsolate(); |
| 49 | + |
| 50 | + if (args[0]->IsArrayBuffer() || args[0]->IsTypedArray()) { |
| 51 | + Local<ArrayBuffer> arraybuffer; |
| 52 | + |
| 53 | + if (args[0]->IsArrayBuffer()) { |
| 54 | + arraybuffer = Local<ArrayBuffer>::Cast(args[0]); |
| 55 | + } else { |
| 56 | + Local<ArrayBufferView> view = Local<ArrayBufferView>::Cast(args[0]); |
| 57 | + arraybuffer = view->Buffer(); |
| 58 | + } |
| 59 | + |
| 60 | + if (!arraybuffer.IsEmpty()) { |
| 61 | + node::ArrayBuffer *buffer = node::ArrayBuffer::New(isolate, arraybuffer); |
| 62 | + |
| 63 | + printf("Buffer Size: '%s', %zu\n", buffer->String(), buffer->Length()); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +void DisposeMemory(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 69 | + Isolate *isolate = args.GetIsolate(); |
| 70 | + |
| 71 | + while(!isolate->IdleNotification(1000)); |
| 72 | +} |
| 73 | + |
| 74 | +void SampleInit(Handle<Object> exports) { |
| 75 | + Isolate* isolate = Isolate::GetCurrent(); |
| 76 | + HandleScope scope(isolate); |
| 77 | + |
| 78 | + exports->Set(String::NewFromUtf8(isolate, "StringArrayBuffer"), |
| 79 | + FunctionTemplate::New(isolate, StringArrayBuffer)->GetFunction()); |
| 80 | + |
| 81 | + exports->Set(String::NewFromUtf8(isolate, "CreateArrayBuffer"), |
| 82 | + FunctionTemplate::New(isolate, CreateArrayBuffer)->GetFunction()); |
| 83 | + |
| 84 | + exports->Set(String::NewFromUtf8(isolate, "PrintWrapped"), |
| 85 | + FunctionTemplate::New(isolate, PrintWrapped)->GetFunction()); |
| 86 | + |
| 87 | + exports->Set(String::NewFromUtf8(isolate, "PrintArrayBuffer"), |
| 88 | + FunctionTemplate::New(isolate, PrintArrayBuffer)->GetFunction()); |
| 89 | + |
| 90 | + exports->Set(String::NewFromUtf8(isolate, "DisposeMemory"), |
| 91 | + FunctionTemplate::New(isolate, DisposeMemory)->GetFunction()); |
| 92 | +} |
| 93 | + |
| 94 | +NODE_MODULE(sample, SampleInit); |
0 commit comments