Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed bug when GC removes dependent buffers, due to lost references #37

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Type definitions for ref-struct
// Project: https://github.com/TooTallNate/ref-struct
// Definitions by: Paul Loyd <https://github.com/loyd>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2

import ref = require('ref');

/**
* This is the `constructor` of the Struct type that gets returned.
*
* Invoke it with `new` to create a new Buffer instance backing the struct.
* Pass it an existing Buffer instance to use that as the backing buffer.
* Pass in an Object containing the struct fields to auto-populate the
* struct with the data.
*
* @constructor
*/
interface StructType extends ref.Type {
/** Pass it an existing Buffer instance to use that as the backing buffer. */
new (arg: Buffer, data?: {}): any;
new (data?: {}): any;
/** Pass it an existing Buffer instance to use that as the backing buffer. */
(arg: Buffer, data?: {}): any;
(data?: {}): any;

fields: { [key: string]: { type: ref.Type } };

/**
* Adds a new field to the struct instance with the given name and type.
* Note that this function will throw an Error if any instances of the struct
* type have already been created, therefore this function must be called at the
* beginning, before any instances are created.
*/
defineProperty(name: string, type: ref.Type): void;

/**
* Adds a new field to the struct instance with the given name and type.
* Note that this function will throw an Error if any instances of the struct
* type have already been created, therefore this function must be called at the
* beginning, before any instances are created.
*/
defineProperty(name: string, type: string): void;

/**
* Custom for struct type instances.
* @override
*/
toString(): string;
}

/** The struct type meta-constructor. */
declare var StructType: {
new (fields?: object, opt?: object): StructType;
new (fields?: any[]): StructType;
(fields?: object, opt?: object): StructType;
(fields?: any[]): StructType;
}

export = StructType;
6 changes: 6 additions & 0 deletions lib/struct.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ function set (buffer, offset, value) {
// optimization: copy the buffer contents directly rather
// than going through the ref-struct constructor
value['ref.buffer'].copy(buffer, offset, 0, this.size)
if (value['ref.buffer']._refs) {
if (!buffer._refs) {
buffer._refs = []
}
buffer._refs = buffer._refs.concat(value['ref.buffer']._refs)
}
} else {
if (offset > 0) {
buffer = buffer.slice(offset)
Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"c++",
"ffi"
],
"version": "1.1.0",
"version": "2.0.1",
"author": "Nathan Rajlich <[email protected]> (http://tootallnate.net)",
"repository": {
"type": "git",
Expand All @@ -18,16 +18,18 @@
"main": "./lib/struct.js",
"license": "MIT",
"scripts": {
"test": "node-gyp rebuild --directory test && mocha -gc --reporter spec"
"test": "node-gyp rebuild --directory test && mocha -gc-global --expose-gc --reporter spec"
},
"dependencies": {
"debug": "2",
"ref": "1"
"debug": "4"
},
"peerDependencies": {
"ref": "*",
"ref-array": "*"
},
"devDependencies": {
"bindings": "~1.2.0",
"bindings": "~1.5.0",
"nan": "2",
"mocha": "*",
"ref-array": "~1.1.2"
"mocha": "*"
}
}