Skip to content

Commit

Permalink
Use ref-array to construct array argument
Browse files Browse the repository at this point in the history
In an attempt to clarify the problem with #3.
  • Loading branch information
sampsyo committed Mar 19, 2017
1 parent 0d9293b commit 0367634
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
},
"dependencies": {
"ffi": "^2.2.0",
"ref": "^1.3.4"
"ref": "^1.3.4",
"ref-array": "^1.2.0"
},
"devDependencies": {
"@types/ffi": "^0.0.19",
"@types/node": "^7.0.5",
"@types/ref": "^0.0.28"
"@types/ref": "^0.0.28",
"@types/ref-array": "^0.0.28"
}
}
9 changes: 8 additions & 1 deletion src/llvmc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import * as ffi from 'ffi';
import * as ref from 'ref';
import * as ArrayType from 'ref-array'
import * as Struct from 'ref-struct';

// Some useful types.
Expand All @@ -20,6 +21,12 @@ let voidp = ref.refType(ref.types.void); // Pointer to an opaque LLVM value.
let voidpp = ref.refType(voidp); // void**, used for arrays and out-parameters.
let void_ = ref.types.void;

/**
* A constructor for arrays of pointers. Use this as `new PointerArray(N)` to
* create an array of length N.
*/
export const PointerArray = ArrayType(ref.refType(ref.types.void));

// some structs
// http://llvm.org/docs/doxygen/html/structLLVMOpInfoSymbol1.html
var LLVMOpInfoSymbol1 = Struct({
Expand Down Expand Up @@ -250,7 +257,7 @@ export const LLVM = ffi.Library('libLLVM', {

// Function types.
// http://llvm.org/docs/doxygen/html/group__LLVMCCoreTypeFunction.html
'LLVMFunctionType': [voidp, [voidp, voidpp, 'uint', 'bool']],
'LLVMFunctionType': [voidp, [voidp, PointerArray, 'uint', 'bool']],
'LLVMIsFunctionVarArg': ['bool', [voidp]],
'LLVMGetReturnType': [voidp, [voidp]],
'LLVMCountParamTypes': ['uint', [voidp]],
Expand Down
16 changes: 10 additions & 6 deletions src/wrapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* TypeScript class instead of an opaque pointer from the API.
*/

import { LLVM } from './llvmc';
import { LLVM, PointerArray } from './llvmc';

/**
* A base class for our wrapper classes that abstract an `LLVM*Ref` type.
Expand Down Expand Up @@ -237,11 +237,15 @@ export class Type extends Ref {
*/
export class FunctionType extends Ref {
static create(ret: Type, params: Type[], isVarArg = false) {
// TODO parameter types ignored currently. We need to transform the
// JavaScript array into a C array of pointers, and then set the length
// parameter to its length (instead of just 0).
//let paramtypes = new Buffer(params.length);
let paramtypes = Buffer.from(params.map(t => t.ref));
// Construct an array containing the parameter references.
let paramtypes = new PointerArray(params.length);
let i = 0;
for (let param of params) {
paramtypes[i] = param.ref;
++i;
}

// Construct the function type.
let ftref = LLVM.LLVMFunctionType(ret.ref, paramtypes, params.length, isVarArg);
return new FunctionType(ftref);
}
Expand Down

0 comments on commit 0367634

Please sign in to comment.