Last active
August 29, 2015 14:10
-
-
Save trevnorris/1d3bd8514d3bbda20e7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var smalloc = require('smalloc'); | |
var uid = 0; | |
function MemStruct() { | |
this._id = 0; // 4 bytes (uint32) | |
this._address = 1; // 4 bytes (uint32) | |
this._data = 2; // 8 bytes (uint64) | |
var size = 4; | |
smalloc.alloc(size, this, smalloc.Types.Uint32); | |
for (var i = 0; i < size; i++) { | |
this[i] = 0; | |
} | |
} | |
MemStruct.prototype.id = function id(n) { | |
if (n == null) | |
return this[(((this._id)))]; | |
return this[(((this._id)))] = n; | |
}; | |
MemStruct.prototype.address = function address(n) { | |
if (n == null) | |
return this[this._address]; | |
return this[this._address] = n; | |
}; | |
// Only works for little-endian. | |
MemStruct.prototype.data = function data(n) { | |
if (n == null) | |
return this[this._data] * 0x100000000 + this[this._data + 1]; | |
if (n <= (-1 >>> 0)) { | |
this[this._data] = 0; | |
return this[this._data + 1] = n; | |
} | |
// figure out later | |
this[this._data] = (n / 0x100000000) >>> 0; | |
this[this._data + 1] = n >>> 0; | |
return this[this._data] * 0x100000000 + this[this._data + 1]; | |
}; | |
/* | |
var id_fn = MemStruct.prototype.id; | |
var id_str = id_fn.toString().substr(17); | |
id_str = id_str.substr(0, id_str.length - 2); | |
id_str = id_str.replace(/\(\(\(this\._id\)\)\)/g, '0'); | |
id_fn = new Function('n', id_str); | |
id_fn.name = 'id'; | |
console.log(id_str); | |
console.log(id_fn.toString()); | |
return; | |
*/ | |
var st = new MemStruct(); | |
console.log(st); | |
//console.log(st.id()); | |
console.log(st.id(++uid)); | |
//console.log(st.address()); | |
console.log(st.address(0xdeadbeef).toString(16)); | |
//console.log(st.data()); | |
console.log(st.data((-1 >>> 0) + 5)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var smalloc = require('smalloc'); | |
function MemStruct() { | |
this._byte_offset = 0; | |
this._created = false; | |
} | |
MemStruct.prototype.set_field = function set_field(name, byte_size, type) { | |
this['_' + name] = this._byte_offset; | |
this._byte_offset += byte_size; | |
//this[name] = /* dynamically create function to get/set that field */ | |
}; | |
MemStruct.prototype.create = function create(should_zero) { | |
if (this._created) | |
return; | |
smalloc.alloc(this._byte_offset, this); | |
this._created = true; | |
if (should_zero) { | |
for (var i = 0; i < this._byte_offset; i++) { | |
this[i] = 0; | |
} | |
} | |
}; | |
var ms = new MemStruct(); | |
ms.set_field('id', 4); | |
ms.set_field('address', 4); | |
ms.set_field('data', 8); | |
ms.create(true); | |
console.log(ms); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void SetupStruct(const FunctionCallbackInfo<Value>& args) { | |
void* data = args[0].As<Object>()->GetIndexedPropertiesExternalArrayData(); | |
size_t len = args[0].As<Object>()->GetIndexedPropertiesExternalArrayDataLength(); | |
Local<Array> ar = args[1].As<Array>(); | |
for (size_t i = 0; i < ar.Length(); i++) { | |
HandleScope scope(args.GetIsolate()); | |
Utf8Value str(ar->Get(0).As<String>()); | |
// str is now a const char* | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment