cwise-compiler

raw JSON →
1.1.3 verified Fri May 01 auth: no javascript maintenance

cwise-compiler is the internal compiler component of the cwise library for ndarray-optimized operations. Version 1.1.3 is the current stable release. It provides a lower-level API to compile cwise procedures directly, bypassing the parser (cwise-parser) and esprima. This package is intended for advanced users who need to skip the standard cwise interface. It compiles a procedure object containing parsed pre, body, and post functions into optimized JavaScript. The main differentiator is its role as a core piece of the cwise ecosystem, offering a minimal, direct compilation path for ndarray operations.

error TypeError: Cannot read property 'body' of undefined
cause Missing or misspelled 'body' field in the procedure object; likely a typo in pre/body/post.
fix
Check that the procedure object has correctly formed pre, body, post sub-objects each with a 'body' string property.
error Error: Invalid args array passed to compiler
cause The args array contains unsupported type strings or numbers.
fix
Ensure args is an array of strings like 'array', 'scalar', 'index', 'shape', etc., as defined by cwise.
error ReferenceError: a is not defined (in compiled function)
cause The body function uses variable 'a' but it is not listed in localVars or args.
fix
Add 'a' to the localVars array of the corresponding pre/body/post object, or ensure it's an argument.
breaking Procedure object must have exactly specified fields; missing or extra fields cause errors.
fix Ensure procedure object includes all required fields: args, pre, body, post, funcName, blockSize, debug.
deprecated This package is deprecated in favor of using the higher-level cwise API directly. Direct use is not recommended.
fix Use cwise package instead of calling cwise-compiler directly.
gotcha The pre, body, post functions must be parsed objects with specific structure (body, args, thisVars, localVars), not raw functions.
fix Use cwise-parser to parse raw function strings into the required format, or ensure you provide correctly structured objects.
gotcha The compiled function expects ndarray arguments in the order specified by args; mismatched types cause runtime errors.
fix Match the args array to the types and order of arrays passed to the compiled function.
npm install cwise-compiler
yarn add cwise-compiler
pnpm add cwise-compiler

Demonstrates compiling a simple procedure that adds one to each element of an ndarray.

const compiler = require('cwise-compiler');

const procedure = {
  args: ['array', 'array'],
  pre: { body: 'function pre() { var a = 0; }', args: [], thisVars: ['a'], localVars: [] },
  body: { body: 'function body(a, b) { a = b + 1; }', args: ['a', 'b'], thisVars: [], localVars: [] },
  post: { body: 'function post() { }', args: [], thisVars: [], localVars: [] },
  funcName: 'addOne',
  blockSize: 64,
  debug: false
};

const compiled = compiler(procedure);
console.log(typeof compiled); // function