Skip to content

Instantly share code, notes, and snippets.

@yosuke-furukawa
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yosuke-furukawa/2ee4f4e51bc2df27e616 to your computer and use it in GitHub Desktop.
Save yosuke-furukawa/2ee4f4e51bc2df27e616 to your computer and use it in GitHub Desktop.

I found weird behavior in Node.js v0.11.13.

First of all, i just want to check v8::Object#HasIndexedPropertiesInExternalArrayData behavior. However I could not get true from the method( always return false...). I tried repeatedly. So I just found the following behavior.

#include <node.h>
#include <nan.h>

using namespace v8;

NAN_METHOD(Method) {
  NanScope();
  // get first argument
  v8::Handle<v8::Object> buffer = args[0]->ToObject();
  // get HasIndexedPropertiesInExternalArrayData value.
  // if buffer has indexed properties like {"0": 123, "1":234, "2":345}, return true.
  bool hasIndexed = buffer->HasIndexedPropertiesInExternalArrayData();
  NanReturnValue(NanNew<Boolean>(hasIndexed));
}

void init(Handle<Object> exports) {
  exports->Set(NanSymbol("hasIndexed"),
      NanNew<FunctionTemplate>(Method)->GetFunction());
}

NODE_MODULE(addon, init)
var hello = require("../hello");

var assert = require("assert");

var arr = new Uint8Array(8);
console.log(arr);
// HasIndexedPropertiesInExternalArrayData is false
console.log(hello.hasIndexed(arr));
// if arr.buffer is called
arr.buffer;
// HasIndexedPropertiesInExternalArrayData is true
console.log(hello.hasIndexed(arr));

v0.10 always returns true regardless of arr.buffer.

Full Code is here https://github.com/yosuke-furukawa/NANSample/tree/test/hasindexed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment