MCPcopy Create free account
hub / github.com/charto/nbind / buildCallerFunction

Function buildCallerFunction

src/em/Caller.ts:119–174  ·  view source on GitHub ↗

Dynamically build a function that calls an Asm.js invoker * with appropriate type conversion for complicated types: * - Push arguments to stack. * - Read return value. * - Restore stack pointer if necessary.

(
		dynCall: Func,
		ptrType: _class.BindClassPtr | null,
		ptr: number,
		num: number,
		policyTbl: PolicyTbl | null,
		needsWireWrite: boolean,
		prefix: string,
		returnType: BindType,
		argTypeList: BindType[],
		mask?: number,
		err?: () => void
	)

Source from the content-addressed store, hash-verified

117 * - Restore stack pointer if necessary. */
118
119 function buildCallerFunction(
120 dynCall: Func,
121 ptrType: _class.BindClassPtr | null,
122 ptr: number,
123 num: number,
124 policyTbl: PolicyTbl | null,
125 needsWireWrite: boolean,
126 prefix: string,
127 returnType: BindType,
128 argTypeList: BindType[],
129 mask?: number,
130 err?: () => void
131 ) {
132 const argList = makeArgList(argTypeList.length);
133 /** List of arbitrary data for type converters.
134 * Each one may read and write its own slot. */
135 const convertParamList: any[] = [];
136
137 // Build code for function call and type conversion.
138
139 const callExpression = makeWireRead(
140 convertParamList,
141 policyTbl,
142 returnType,
143 'dynCall(' +
144 [prefix].concat(argList.map(
145 // TODO: if one wireWrite throws,
146 // resources allocated by others may leak!
147 (name: string, index: number) => makeWireWrite(
148 convertParamList,
149 policyTbl,
150 argTypeList[index],
151 name
152 )
153 )).join(',') +
154 ')'
155 );
156
157 // Build code to allocate and free the stack etc. if necessary.
158
159 const resourceSet = listResources([returnType], argTypeList);
160
161 const sourceCode = (
162 'function(' + argList.join(',') + '){' +
163 (mask ? 'this.__nbindFlags&mask&&err();' : '') +
164 resourceSet.makeOpen() +
165 'var r=' + callExpression + ';' +
166 resourceSet.makeClose() +
167 'return r;' +
168 '}'
169 );
170
171 // Use eval to allow JIT compiling the function.
172
173 return(eval('(' + sourceCode + ')') as (...args: any[]) => any);
174 }
175
176 /** Dynamically build a function that calls a JavaScript callback invoker

Callers 2

makeMethodCallerFunction · 0.85
makeCallerFunction · 0.85

Calls 4

makeArgListFunction · 0.85
makeWireReadFunction · 0.85
makeWireWriteFunction · 0.85
listResourcesFunction · 0.85

Tested by

no test coverage detected