Skip to content

Instantly share code, notes, and snippets.

@joshuachinemezu
Created April 14, 2022 07:57
Show Gist options
  • Save joshuachinemezu/9da68e14f702e14ee581e55e1d1a10b2 to your computer and use it in GitHub Desktop.
Save joshuachinemezu/9da68e14f702e14ee581e55e1d1a10b2 to your computer and use it in GitHub Desktop.
/**
* gpu.js
* http://gpu.rocks/
*
* GPU Accelerated JavaScript
*
* @version 1.0.0-rc.10
* @date Sat Dec 23 2017 10:30:09 GMT-0500 (EST)
*
* @license MIT
* The MIT License
*
* Copyright (c) 2017 gpu.js Team
*/
'use strict'
;(function e(t, n, r) {
function s(o, u) {
if (!n[o]) {
if (!t[o]) {
var a = typeof require == 'function' && require
if (!u && a) return a(o, !0)
if (i) return i(o, !0)
var f = new Error("Cannot find module '" + o + "'")
throw ((f.code = 'MODULE_NOT_FOUND'), f)
}
var l = (n[o] = { exports: {} })
t[o][0].call(
l.exports,
function (e) {
var n = t[o][1][e]
return s(n ? n : e)
},
l,
l.exports,
e,
t,
n,
r,
)
}
return n[o].exports
}
var i = typeof require == 'function' && require
for (var o = 0; o < r.length; o++) s(r[o])
return s
})(
{
1: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called",
)
}
return call &&
(typeof call === 'object' || typeof call === 'function')
? call
: self
}
function _inherits(subClass, superClass) {
if (
typeof superClass !== 'function' &&
superClass !== null
) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass,
)
}
subClass.prototype = Object.create(
superClass && superClass.prototype,
{
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true,
},
},
)
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass)
}
var FunctionBuilderBase = require('../function-builder-base')
var CPUFunctionNode = require('./function-node')
module.exports = (function (_FunctionBuilderBase) {
_inherits(CPUFunctionBuilder, _FunctionBuilderBase)
function CPUFunctionBuilder() {
_classCallCheck(this, CPUFunctionBuilder)
var _this = _possibleConstructorReturn(
this,
(
CPUFunctionBuilder.__proto__ ||
Object.getPrototypeOf(CPUFunctionBuilder)
).call(this),
)
_this.Node = CPUFunctionNode
return _this
}
_createClass(CPUFunctionBuilder, [
{
key: 'polyfillStandardFunctions',
value: function polyfillStandardFunctions() {},
},
])
return CPUFunctionBuilder
})(FunctionBuilderBase)
},
{ '../function-builder-base': 6, './function-node': 2 },
],
2: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called",
)
}
return call &&
(typeof call === 'object' || typeof call === 'function')
? call
: self
}
function _inherits(subClass, superClass) {
if (
typeof superClass !== 'function' &&
superClass !== null
) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass,
)
}
subClass.prototype = Object.create(
superClass && superClass.prototype,
{
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true,
},
},
)
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass)
}
var BaseFunctionNode = require('../function-node-base')
var utils = require('../../core/utils')
module.exports = (function (_BaseFunctionNode) {
_inherits(CPUFunctionNode, _BaseFunctionNode)
function CPUFunctionNode() {
_classCallCheck(this, CPUFunctionNode)
return _possibleConstructorReturn(
this,
(
CPUFunctionNode.__proto__ ||
Object.getPrototypeOf(CPUFunctionNode)
).apply(this, arguments),
)
}
_createClass(
CPUFunctionNode,
[
{
key: 'generate',
value: function generate() {
if (this.debug) {
console.log(this)
}
this.functionStringArray = this.astGeneric(
this.getJsAST(),
[],
this,
)
this.functionString =
this.functionStringArray.join('').trim()
return this.functionString
},
},
{
key: 'getFunctionPrototypeString',
value: function getFunctionPrototypeString() {
if (this.webGlFunctionPrototypeString) {
return this.webGlFunctionPrototypeString
}
return (this.webGlFunctionPrototypeString =
this.generate())
},
},
{
key: 'astGeneric',
value: function astGeneric(
ast,
retArr,
funcParam,
) {
if (ast === null) {
throw this.astErrorOutput(
'NULL ast',
ast,
funcParam,
)
} else {
if (Array.isArray(ast)) {
for (
var i = 0;
i < ast.length;
i++
) {
this.astGeneric(
ast[i],
retArr,
funcParam,
)
}
return retArr
}
switch (ast.type) {
case 'FunctionDeclaration':
return this.astFunctionDeclaration(
ast,
retArr,
funcParam,
)
case 'FunctionExpression':
return this.astFunctionExpression(
ast,
retArr,
funcParam,
)
case 'ReturnStatement':
return this.astReturnStatement(
ast,
retArr,
funcParam,
)
case 'Literal':
return this.astLiteral(
ast,
retArr,
funcParam,
)
case 'BinaryExpression':
return this.astBinaryExpression(
ast,
retArr,
funcParam,
)
case 'Identifier':
return this.astIdentifierExpression(
ast,
retArr,
funcParam,
)
case 'AssignmentExpression':
return this.astAssignmentExpression(
ast,
retArr,
funcParam,
)
case 'ExpressionStatement':
return this.astExpressionStatement(
ast,
retArr,
funcParam,
)
case 'EmptyStatement':
return this.astEmptyStatement(
ast,
retArr,
funcParam,
)
case 'BlockStatement':
return this.astBlockStatement(
ast,
retArr,
funcParam,
)
case 'IfStatement':
return this.astIfStatement(
ast,
retArr,
funcParam,
)
case 'BreakStatement':
return this.astBreakStatement(
ast,
retArr,
funcParam,
)
case 'ContinueStatement':
return this.astContinueStatement(
ast,
retArr,
funcParam,
)
case 'ForStatement':
return this.astForStatement(
ast,
retArr,
funcParam,
)
case 'WhileStatement':
return this.astWhileStatement(
ast,
retArr,
funcParam,
)
case 'VariableDeclaration':
return this.astVariableDeclaration(
ast,
retArr,
funcParam,
)
case 'VariableDeclarator':
return this.astVariableDeclarator(
ast,
retArr,
funcParam,
)
case 'ThisExpression':
return this.astThisExpression(
ast,
retArr,
funcParam,
)
case 'SequenceExpression':
return this.astSequenceExpression(
ast,
retArr,
funcParam,
)
case 'UnaryExpression':
return this.astUnaryExpression(
ast,
retArr,
funcParam,
)
case 'UpdateExpression':
return this.astUpdateExpression(
ast,
retArr,
funcParam,
)
case 'LogicalExpression':
return this.astLogicalExpression(
ast,
retArr,
funcParam,
)
case 'MemberExpression':
return this.astMemberExpression(
ast,
retArr,
funcParam,
)
case 'CallExpression':
return this.astCallExpression(
ast,
retArr,
funcParam,
)
case 'ArrayExpression':
return this.astArrayExpression(
ast,
retArr,
funcParam,
)
case 'DebuggerStatement':
return this.astDebuggerStatement(
ast,
retArr,
funcParam,
)
}
throw this.astErrorOutput(
'Unknown ast type : ' + ast.type,
ast,
funcParam,
)
}
},
},
{
key: 'astFunctionDeclaration',
value: function astFunctionDeclaration(
ast,
retArr,
funcParam,
) {
if (this.addFunction) {
this.addFunction(
null,
utils.getAstString(
this.jsFunctionString,
ast,
),
)
}
return retArr
},
},
{
key: 'astFunctionExpression',
value: function astFunctionExpression(
ast,
retArr,
funcParam,
) {
if (!funcParam.isRootKernel) {
retArr.push('function')
funcParam.kernalAst = ast
retArr.push(' ')
retArr.push(funcParam.functionName)
retArr.push('(')
for (
var i = 0;
i < funcParam.paramNames.length;
++i
) {
var paramName =
funcParam.paramNames[i]
if (i > 0) {
retArr.push(', ')
}
retArr.push(' ')
retArr.push('user_')
retArr.push(paramName)
}
retArr.push(') {\n')
}
for (
var _i = 0;
_i < ast.body.body.length;
++_i
) {
this.astGeneric(
ast.body.body[_i],
retArr,
funcParam,
)
retArr.push('\n')
}
if (!funcParam.isRootKernel) {
retArr.push('}\n')
}
return retArr
},
},
{
key: 'astReturnStatement',
value: function astReturnStatement(
ast,
retArr,
funcParam,
) {
if (funcParam.isRootKernel) {
retArr.push('kernelResult = ')
this.astGeneric(
ast.argument,
retArr,
funcParam,
)
retArr.push(';')
} else if (funcParam.isSubKernel) {
retArr.push(
funcParam.functionName +
'Result = ',
)
this.astGeneric(
ast.argument,
retArr,
funcParam,
)
retArr.push(';')
retArr.push(
'return ' +
funcParam.functionName +
'Result;',
)
} else {
retArr.push('return ')
this.astGeneric(
ast.argument,
retArr,
funcParam,
)
retArr.push(';')
}
return retArr
},
},
{
key: 'astLiteral',
value: function astLiteral(
ast,
retArr,
funcParam,
) {
if (isNaN(ast.value)) {
throw this.astErrorOutput(
'Non-numeric literal not supported : ' +
ast.value,
ast,
funcParam,
)
}
retArr.push(ast.value)
return retArr
},
},
{
key: 'astBinaryExpression',
value: function astBinaryExpression(
ast,
retArr,
funcParam,
) {
retArr.push('(')
this.astGeneric(ast.left, retArr, funcParam)
retArr.push(ast.operator)
this.astGeneric(
ast.right,
retArr,
funcParam,
)
retArr.push(')')
return retArr
},
},
{
key: 'astIdentifierExpression',
value: function astIdentifierExpression(
idtNode,
retArr,
funcParam,
) {
if (idtNode.type !== 'Identifier') {
throw this.astErrorOutput(
'IdentifierExpression - not an Identifier',
idtNode,
funcParam,
)
}
switch (idtNode.name) {
case 'gpu_threadX':
retArr.push('threadId.x')
break
case 'gpu_threadY':
retArr.push('threadId.y')
break
case 'gpu_threadZ':
retArr.push('threadId.z')
break
case 'gpu_outputX':
retArr.push('uOutputDim.x')
break
case 'gpu_outputY':
retArr.push('uOutputDim.y')
break
case 'gpu_outputZ':
retArr.push('uOutputDim.z')
break
default:
if (
this.constants &&
this.constants.hasOwnProperty(
idtNode.name,
)
) {
retArr.push(
'constants_' + idtNode.name,
)
} else {
var userParamName =
funcParam.getUserParamName(
idtNode.name,
)
if (userParamName !== null) {
retArr.push(
'user_' + userParamName,
)
} else {
retArr.push(
'user_' + idtNode.name,
)
}
}
}
return retArr
},
},
{
key: 'astForStatement',
value: function astForStatement(
forNode,
retArr,
funcParam,
) {
if (forNode.type !== 'ForStatement') {
throw this.astErrorOutput(
'Invalid for statment',
forNode,
funcParam,
)
}
if (
forNode.test &&
forNode.test.type === 'BinaryExpression'
) {
if (
(forNode.test.right.type ===
'Identifier' ||
forNode.test.right.type ===
'Literal') &&
forNode.test.operator === '<' &&
this.isIdentifierConstant(
forNode.test.right.name,
) === false
) {
if (!this.loopMaxIterations) {
console.warn(
'Warning: loopMaxIterations is not set! Using default of 1000 which may result in unintended behavior.',
)
console.warn(
'Set loopMaxIterations or use a for loop of fixed length to silence this message.',
)
}
retArr.push('for (')
this.astGeneric(
forNode.init,
retArr,
funcParam,
)
if (
retArr[retArr.length - 1] !==
';'
) {
retArr.push(';')
}
this.astGeneric(
forNode.test.left,
retArr,
funcParam,
)
retArr.push(forNode.test.operator)
retArr.push('LOOP_MAX')
retArr.push(';')
this.astGeneric(
forNode.update,
retArr,
funcParam,
)
retArr.push(')')
retArr.push('{\n')
retArr.push('if (')
this.astGeneric(
forNode.test.left,
retArr,
funcParam,
)
retArr.push(forNode.test.operator)
this.astGeneric(
forNode.test.right,
retArr,
funcParam,
)
retArr.push(') {\n')
if (
forNode.body.type ===
'BlockStatement'
) {
for (
var i = 0;
i <
forNode.body.body.length;
i++
) {
this.astGeneric(
forNode.body.body[i],
retArr,
funcParam,
)
}
} else {
this.astGeneric(
forNode.body,
retArr,
funcParam,
)
}
retArr.push('} else {\n')
retArr.push('break;\n')
retArr.push('}\n')
retArr.push('}\n')
return retArr
} else if (forNode.init.declarations) {
var declarations = JSON.parse(
JSON.stringify(
forNode.init.declarations,
),
)
var updateArgument =
forNode.update.argument
if (
!Array.isArray(declarations) ||
declarations.length < 1
) {
console.log(
this.jsFunctionString,
)
throw new Error(
'Error: Incompatible for loop declaration',
)
}
if (declarations.length > 1) {
var initArgument = null
for (
var _i2 = 0;
_i2 < declarations.length;
_i2++
) {
var declaration =
declarations[_i2]
if (
declaration.id.name ===
updateArgument.name
) {
initArgument =
declaration
declarations.splice(
_i2,
1,
)
} else {
retArr.push('var ')
this.astGeneric(
declaration,
retArr,
funcParam,
)
retArr.push(';')
}
}
retArr.push('for (let ')
this.astGeneric(
initArgument,
retArr,
funcParam,
)
retArr.push(';')
} else {
retArr.push('for (')
this.astGeneric(
forNode.init,
retArr,
funcParam,
)
}
this.astGeneric(
forNode.test,
retArr,
funcParam,
)
retArr.push(';')
this.astGeneric(
forNode.update,
retArr,
funcParam,
)
retArr.push(')')
this.astGeneric(
forNode.body,
retArr,
funcParam,
)
return retArr
}
}
throw this.astErrorOutput(
'Invalid for statement',
forNode,
funcParam,
)
},
},
{
key: 'astWhileStatement',
value: function astWhileStatement(
whileNode,
retArr,
funcParam,
) {
if (whileNode.type !== 'WhileStatement') {
throw this.astErrorOutput(
'Invalid while statment',
whileNode,
funcParam,
)
}
retArr.push(
'for (let i = 0; i < LOOP_MAX; i++) {',
)
retArr.push('if (')
this.astGeneric(
whileNode.test,
retArr,
funcParam,
)
retArr.push(') {\n')
this.astGeneric(
whileNode.body,
retArr,
funcParam,
)
retArr.push('} else {\n')
retArr.push('break;\n')
retArr.push('}\n')
retArr.push('}\n')
return retArr
},
},
{
key: 'astAssignmentExpression',
value: function astAssignmentExpression(
assNode,
retArr,
funcParam,
) {
this.astGeneric(
assNode.left,
retArr,
funcParam,
)
retArr.push(assNode.operator)
this.astGeneric(
assNode.right,
retArr,
funcParam,
)
return retArr
},
},
{
key: 'astEmptyStatement',
value: function astEmptyStatement(
eNode,
retArr,
funcParam,
) {
return retArr
},
},
{
key: 'astBlockStatement',
value: function astBlockStatement(
bNode,
retArr,
funcParam,
) {
retArr.push('{\n')
for (
var i = 0;
i < bNode.body.length;
i++
) {
this.astGeneric(
bNode.body[i],
retArr,
funcParam,
)
}
retArr.push('}\n')
return retArr
},
},
{
key: 'astExpressionStatement',
value: function astExpressionStatement(
esNode,
retArr,
funcParam,
) {
this.astGeneric(
esNode.expression,
retArr,
funcParam,
)
retArr.push(';\n')
return retArr
},
},
{
key: 'astVariableDeclaration',
value: function astVariableDeclaration(
vardecNode,
retArr,
funcParam,
) {
retArr.push('var ')
for (
var i = 0;
i < vardecNode.declarations.length;
i++
) {
if (i > 0) {
retArr.push(',')
}
this.astGeneric(
vardecNode.declarations[i],
retArr,
funcParam,
)
}
retArr.push(';')
return retArr
},
},
{
key: 'astVariableDeclarator',
value: function astVariableDeclarator(
ivardecNode,
retArr,
funcParam,
) {
this.astGeneric(
ivardecNode.id,
retArr,
funcParam,
)
if (ivardecNode.init !== null) {
retArr.push('=')
this.astGeneric(
ivardecNode.init,
retArr,
funcParam,
)
}
return retArr
},
},
{
key: 'astIfStatement',
value: function astIfStatement(
ifNode,
retArr,
funcParam,
) {
retArr.push('if (')
this.astGeneric(
ifNode.test,
retArr,
funcParam,
)
retArr.push(')')
if (
ifNode.consequent.type ===
'BlockStatement'
) {
this.astGeneric(
ifNode.consequent,
retArr,
funcParam,
)
} else {
retArr.push(' {\n')
this.astGeneric(
ifNode.consequent,
retArr,
funcParam,
)
retArr.push('\n}\n')
}
if (ifNode.alternate) {
retArr.push('else ')
if (
ifNode.alternate.type ===
'BlockStatement'
) {
this.astGeneric(
ifNode.alternate,
retArr,
funcParam,
)
} else {
retArr.push(' {\n')
this.astGeneric(
ifNode.alternate,
retArr,
funcParam,
)
retArr.push('\n}\n')
}
}
return retArr
},
},
{
key: 'astBreakStatement',
value: function astBreakStatement(
brNode,
retArr,
funcParam,
) {
retArr.push('break;\n')
return retArr
},
},
{
key: 'astContinueStatement',
value: function astContinueStatement(
crNode,
retArr,
funcParam,
) {
retArr.push('continue;\n')
return retArr
},
},
{
key: 'astLogicalExpression',
value: function astLogicalExpression(
logNode,
retArr,
funcParam,
) {
retArr.push('(')
this.astGeneric(
logNode.left,
retArr,
funcParam,
)
retArr.push(logNode.operator)
this.astGeneric(
logNode.right,
retArr,
funcParam,
)
retArr.push(')')
return retArr
},
},
{
key: 'astUpdateExpression',
value: function astUpdateExpression(
uNode,
retArr,
funcParam,
) {
if (uNode.prefix) {
retArr.push(uNode.operator)
this.astGeneric(
uNode.argument,
retArr,
funcParam,
)
} else {
this.astGeneric(
uNode.argument,
retArr,
funcParam,
)
retArr.push(uNode.operator)
}
return retArr
},
},
{
key: 'astUnaryExpression',
value: function astUnaryExpression(
uNode,
retArr,
funcParam,
) {
if (uNode.prefix) {
retArr.push(uNode.operator)
this.astGeneric(
uNode.argument,
retArr,
funcParam,
)
} else {
this.astGeneric(
uNode.argument,
retArr,
funcParam,
)
retArr.push(uNode.operator)
}
return retArr
},
},
{
key: 'astThisExpression',
value: function astThisExpression(
tNode,
retArr,
funcParam,
) {
retArr.push('_this')
return retArr
},
},
{
key: 'astMemberExpression',
value: function astMemberExpression(
mNode,
retArr,
funcParam,
) {
if (mNode.computed) {
if (
mNode.object.type === 'Identifier'
) {
this.astGeneric(
mNode.object,
retArr,
funcParam,
)
retArr.push('[')
this.astGeneric(
mNode.property,
retArr,
funcParam,
)
retArr.push(']')
} else {
this.astGeneric(
mNode.object,
retArr,
funcParam,
)
var last = retArr.pop()
retArr.push('][')
this.astGeneric(
mNode.property,
retArr,
funcParam,
)
retArr.push(last)
}
} else {
var unrolled =
this.astMemberExpressionUnroll(
mNode,
)
if (
mNode.property.type ===
'Identifier' &&
mNode.computed
) {
unrolled = 'user_' + unrolled
}
if (unrolled.indexOf('this') === 0) {
unrolled = '_' + unrolled
}
switch (unrolled) {
case '_this.output.x':
retArr.push(this.output[0])
break
case '_this.output.y':
retArr.push(this.output[1])
break
case '_this.output.z':
retArr.push(this.output[2])
break
default:
retArr.push(unrolled)
}
}
return retArr
},
},
{
key: 'astSequenceExpression',
value: function astSequenceExpression(
sNode,
retArr,
funcParam,
) {
for (
var i = 0;
i < sNode.expressions.length;
i++
) {
if (i > 0) {
retArr.push(',')
}
this.astGeneric(
sNode.expressions,
retArr,
funcParam,
)
}
return retArr
},
},
{
key: 'astCallExpression',
value: function astCallExpression(
ast,
retArr,
funcParam,
) {
if (ast.callee) {
var funcName =
this.astMemberExpressionUnroll(
ast.callee,
)
if (
funcParam.calledFunctions.indexOf(
funcName,
) < 0
) {
funcParam.calledFunctions.push(
funcName,
)
}
if (
!funcParam.hasOwnProperty(
'funcName',
)
) {
funcParam.calledFunctionsArguments[
funcName
] = []
}
var functionArguments = []
funcParam.calledFunctionsArguments[
funcName
].push(functionArguments)
retArr.push(funcName)
retArr.push('(')
for (
var i = 0;
i < ast.arguments.length;
++i
) {
var argument = ast.arguments[i]
if (i > 0) {
retArr.push(', ')
}
this.astGeneric(
argument,
retArr,
funcParam,
)
if (
argument.type === 'Identifier'
) {
var paramIndex =
funcParam.paramNames.indexOf(
argument.name,
)
if (paramIndex === -1) {
functionArguments.push(null)
} else {
functionArguments.push({
name: argument.name,
type: funcParam
.paramTypes[
paramIndex
],
})
}
} else {
functionArguments.push(null)
}
}
retArr.push(')')
return retArr
}
throw this.astErrorOutput(
'Unknown CallExpression',
ast,
funcParam,
)
return retArr
},
},
{
key: 'astArrayExpression',
value: function astArrayExpression(
arrNode,
retArr,
funcParam,
) {
var arrLen = arrNode.elements.length
retArr.push('new Float32Array(')
for (var i = 0; i < arrLen; ++i) {
if (i > 0) {
retArr.push(', ')
}
var subNode = arrNode.elements[i]
this.astGeneric(
subNode,
retArr,
funcParam,
)
}
retArr.push(')')
return retArr
},
},
{
key: 'astDebuggerStatement',
value: function astDebuggerStatement(
arrNode,
retArr,
funcParam,
) {
retArr.push('debugger;')
return retArr
},
},
],
[
{
key: 'astFunctionPrototype',
value: function astFunctionPrototype(
ast,
retArr,
funcParam,
) {
if (
funcParam.isRootKernel ||
funcParam.isSubKernel
) {
return retArr
}
retArr.push(funcParam.returnType)
retArr.push(' ')
retArr.push(funcParam.functionName)
retArr.push('(')
for (
var i = 0;
i < funcParam.paramNames.length;
++i
) {
if (i > 0) {
retArr.push(', ')
}
retArr.push(funcParam.paramTypes[i])
retArr.push(' ')
retArr.push('user_')
retArr.push(funcParam.paramNames[i])
}
retArr.push(');\n')
return retArr
},
},
],
)
return CPUFunctionNode
})(BaseFunctionNode)
},
{ '../../core/utils': 25, '../function-node-base': 7 },
],
3: [
function (require, module, exports) {
'use strict'
var utils = require('../../core/utils')
var kernelRunShortcut = require('../kernel-run-shortcut')
module.exports = function (cpuKernel, name) {
return (
'() => {\n ' +
kernelRunShortcut.toString() +
';\n const utils = {\n allPropertiesOf: function ' +
utils.allPropertiesOf.toString() +
',\n clone: function ' +
utils.clone.toString() +
',\n /*splitArray: function ' +
utils.splitArray.toString() +
',\n getArgumentType: function ' +
utils.getArgumentType.toString() +
',\n getOutput: function ' +
utils.getOutput.toString() +
',\n dimToTexSize: function ' +
utils.dimToTexSize.toString() +
',\n copyFlatten: function ' +
utils.copyFlatten.toString() +
',\n flatten: function ' +
utils.flatten.toString() +
",\n systemEndianness: '" +
utils.systemEndianness() +
"',\n initWebGl: function " +
utils.initWebGl.toString() +
',\n isArray: function ' +
utils.isArray.toString() +
'*/\n };\n class ' +
(name || 'Kernel') +
' {\n constructor() { \n this.argumentsLength = 0;\n this._canvas = null;\n this._webGl = null;\n this.built = false;\n this.program = null;\n this.paramNames = ' +
JSON.stringify(cpuKernel.paramNames) +
';\n this.paramTypes = ' +
JSON.stringify(cpuKernel.paramTypes) +
';\n this.texSize = ' +
JSON.stringify(cpuKernel.texSize) +
';\n this.output = ' +
JSON.stringify(cpuKernel.output) +
';\n this._kernelString = `' +
cpuKernel._kernelString +
'`;\n this.output = ' +
JSON.stringify(cpuKernel.output) +
';\n\t\t this.run = function() {\n this.run = null;\n this.build();\n return this.run.apply(this, arguments);\n }.bind(this);\n this.thread = {\n x: 0,\n y: 0,\n z: 0\n };\n }\n setCanvas(canvas) { this._canvas = canvas; return this; }\n setWebGl(webGl) { this._webGl = webGl; return this; }\n ' +
cpuKernel.build.toString() +
'\n run () { ' +
cpuKernel.kernelString +
' }\n getKernelString() { return this._kernelString; }\n };\n return kernelRunShortcut(new Kernel());\n };'
)
}
},
{ '../../core/utils': 25, '../kernel-run-shortcut': 9 },
],
4: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called",
)
}
return call &&
(typeof call === 'object' || typeof call === 'function')
? call
: self
}
function _inherits(subClass, superClass) {
if (
typeof superClass !== 'function' &&
superClass !== null
) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass,
)
}
subClass.prototype = Object.create(
superClass && superClass.prototype,
{
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true,
},
},
)
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass)
}
var KernelBase = require('../kernel-base')
var utils = require('../../core/utils')
var kernelString = require('./kernel-string')
module.exports = (function (_KernelBase) {
_inherits(CPUKernel, _KernelBase)
function CPUKernel(fnString, settings) {
_classCallCheck(this, CPUKernel)
var _this = _possibleConstructorReturn(
this,
(
CPUKernel.__proto__ ||
Object.getPrototypeOf(CPUKernel)
).call(this, fnString, settings),
)
_this._fnBody =
utils.getFunctionBodyFromString(fnString)
_this._fn = null
_this.run = null
_this._canvasCtx = null
_this._imageData = null
_this._colorData = null
_this._kernelString = null
_this.thread = {
x: 0,
y: 0,
z: 0,
}
_this.run = function () {
this.run = null
this.build.apply(this, arguments)
return this.run.apply(this, arguments)
}.bind(_this)
return _this
}
_createClass(
CPUKernel,
[
{
key: 'validateOptions',
value: function validateOptions() {
if (
!this.output ||
this.output.length === 0
) {
if (arguments.length !== 1) {
throw 'Auto dimensions only supported for kernels with only one input'
}
var argType = utils.getArgumentType(
arguments[0],
)
if (argType === 'Array') {
this.output =
utils.getDimensions(argType)
} else if (argType === 'Texture') {
this.output = arguments[0].output
} else {
throw (
'Auto dimensions not supported for input type: ' +
argType
)
}
}
},
},
{
key: 'build',
value: function build() {
this.setupParams(arguments)
var threadDim = (this.threadDim =
utils.clone(this.output))
while (threadDim.length < 3) {
threadDim.push(1)
}
if (this.graphical) {
var canvas = this.getCanvas()
canvas.width = threadDim[0]
canvas.height = threadDim[1]
this._canvasCtx =
canvas.getContext('2d')
this._imageData =
this._canvasCtx.createImageData(
threadDim[0],
threadDim[1],
)
this._colorData = new Uint8ClampedArray(
threadDim[0] * threadDim[1] * 4,
)
}
var kernelString = this.getKernelString()
if (this.debug) {
console.log('Options:')
console.dir(this)
console.log('Function output:')
console.log(kernelString)
}
this.kernelString = kernelString
this.run = new Function(
[],
kernelString,
).bind(this)()
},
},
{
key: 'color',
value: function color(r, g, b, a) {
if (typeof a === 'undefined') {
a = 1
}
r = Math.floor(r * 255)
g = Math.floor(g * 255)
b = Math.floor(b * 255)
a = Math.floor(a * 255)
var width = this.output[0]
var height = this.output[1]
var x = this.thread.x
var y = height - this.thread.y - 1
var index = x + y * width
this._colorData[index * 4 + 0] = r
this._colorData[index * 4 + 1] = g
this._colorData[index * 4 + 2] = b
this._colorData[index * 4 + 3] = a
},
},
{
key: 'getKernelString',
value: function getKernelString() {
var _this2 = this
if (this._kernelString !== null)
return this._kernelString
var builder = this.functionBuilder
var threadDim =
this.threadDim ||
(this.threadDim = utils.clone(
this.output,
))
while (threadDim.length < 3) {
threadDim.push(1)
}
builder.addKernel(
this.fnString,
{
prototypeOnly: false,
constants: this.constants,
output: this.output,
debug: this.debug,
loopMaxIterations:
this.loopMaxIterations,
},
this.paramNames,
this.paramTypes,
)
builder.addFunctions(this.functions, {
constants: this.constants,
output: this.output,
})
if (this.subKernels !== null) {
this.subKernelOutputTextures = []
this.subKernelOutputVariableNames = []
for (
var i = 0;
i < this.subKernels.length;
i++
) {
var subKernel = this.subKernels[i]
builder.addSubKernel(subKernel, {
prototypeOnly: false,
constants: this.constants,
output: this.output,
debug: this.debug,
loopMaxIterations:
this.loopMaxIterations,
})
this.subKernelOutputVariableNames.push(
subKernel.name + 'Result',
)
}
} else if (
this.subKernelProperties !== null
) {
this.subKernelOutputVariableNames = []
var _i = 0
for (var p in this
.subKernelProperties) {
if (
!this.subKernelProperties.hasOwnProperty(
p,
)
)
continue
var _subKernel =
this.subKernelProperties[p]
builder.addSubKernel(_subKernel)
this.subKernelOutputVariableNames.push(
_subKernel.name + 'Result',
)
_i++
}
}
var prototypes = builder.getPrototypes()
var kernel = prototypes.shift()
var kernelString = (this._kernelString =
'\n\t\tvar LOOP_MAX = ' +
this._getLoopMaxString() +
';\n\t\tvar _this = this;\n ' +
(this.subKernelOutputVariableNames ===
null
? ''
: this.subKernelOutputVariableNames
.map(function (name) {
return (
' var ' +
name +
' = null;\n'
)
})
.join('')) +
'\n return function (' +
this.paramNames
.map(function (paramName) {
return 'user_' + paramName
})
.join(', ') +
') {\n var ret = new Array(' +
threadDim[2] +
');\n ' +
(this.subKernelOutputVariableNames ===
null
? ''
: this.subKernelOutputVariableNames
.map(function (name) {
return (
' ' +
name +
'Z = new Array(' +
threadDim[2] +
');\n'
)
})
.join('')) +
'\n for (this.thread.z = 0; this.thread.z < ' +
threadDim[2] +
'; this.thread.z++) {\n ret[this.thread.z] = new Array(' +
threadDim[1] +
');\n ' +
(this.subKernelOutputVariableNames ===
null
? ''
: this.subKernelOutputVariableNames
.map(function (name) {
return (
' ' +
name +
'Z[this.thread.z] = new Array(' +
threadDim[1] +
');\n'
)
})
.join('')) +
'\n for (this.thread.y = 0; this.thread.y < ' +
threadDim[1] +
'; this.thread.y++) {\n ret[this.thread.z][this.thread.y] = new Array(' +
threadDim[0] +
');\n ' +
(this.subKernelOutputVariableNames ===
null
? ''
: this.subKernelOutputVariableNames
.map(function (name) {
return (
' ' +
name +
'Z[this.thread.z][this.thread.y] = new Array(' +
threadDim[0] +
');\n'
)
})
.join('')) +
'\n for (this.thread.x = 0; this.thread.x < ' +
threadDim[0] +
'; this.thread.x++) {\n var kernelResult;\n ' +
kernel +
'\n ret[this.thread.z][this.thread.y][this.thread.x] = kernelResult;\n' +
(this.subKernelOutputVariableNames ===
null
? ''
: this.subKernelOutputVariableNames
.map(function (name) {
return (
' ' +
name +
'Z[this.thread.z][this.thread.y][this.thread.x] = ' +
name +
';\n'
)
})
.join('')) +
'\n }\n }\n }\n \n if (this.graphical) {\n this._imageData.data.set(this._colorData);\n this._canvasCtx.putImageData(this._imageData, 0, 0);\n return;\n }\n \n if (this.output.length === 1) {\n ret = ret[0][0];\n' +
(this.subKernelOutputVariableNames ===
null
? ''
: this.subKernelOutputVariableNames
.map(function (name) {
return (
' ' +
name +
' = ' +
name +
'Z[0][0];\n'
)
})
.join('')) +
'\n \n } else if (this.output.length === 2) {\n ret = ret[0];\n ' +
(this.subKernelOutputVariableNames ===
null
? ''
: this.subKernelOutputVariableNames
.map(function (name) {
return (
' ' +
name +
' = ' +
name +
'Z[0];\n'
)
})
.join('')) +
'\n }\n \n ' +
(this.subKernelOutputVariableNames ===
null
? 'return ret;\n'
: this.subKernels !== null
? 'var result = [\n ' +
this.subKernelOutputVariableNames
.map(function (name) {
return '' + name
})
.join(',\n') +
'\n ];\n result.result = ret;\n return result;\n'
: 'return {\n result: ret,\n ' +
Object.keys(
this.subKernelProperties,
)
.map(function (name, i) {
return (
name +
': ' +
_this2
.subKernelOutputVariableNames[
i
]
)
})
.join(',\n') +
'\n };') +
'\n ' +
(prototypes.length > 0
? prototypes.join('\n')
: '') +
'\n }.bind(this);')
return kernelString
},
},
{
key: 'toString',
value: function toString() {
return kernelString(this)
},
},
{
key: 'precompileKernelObj',
value: function precompileKernelObj(argTypes) {
var threadDim =
this.threadDim ||
(this.threadDim = utils.clone(
this.output,
))
return {
threadDim: threadDim,
}
},
},
{
key: '_getLoopMaxString',
value: function _getLoopMaxString() {
return this.loopMaxIterations
? ' ' +
parseInt(
this.loopMaxIterations,
) +
';\n'
: ' 1000;\n'
},
},
],
[
{
key: 'compileKernel',
value: function compileKernel(precompileObj) {
var threadDim = precompileObj.threadDim
while (threadDim.length < 3) {
threadDim.push(1)
}
},
},
],
)
return CPUKernel
})(KernelBase)
},
{
'../../core/utils': 25,
'../kernel-base': 8,
'./kernel-string': 3,
},
],
5: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called",
)
}
return call &&
(typeof call === 'object' || typeof call === 'function')
? call
: self
}
function _inherits(subClass, superClass) {
if (
typeof superClass !== 'function' &&
superClass !== null
) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass,
)
}
subClass.prototype = Object.create(
superClass && superClass.prototype,
{
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true,
},
},
)
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass)
}
var utils = require('../../core/utils')
var RunnerBase = require('../runner-base')
var CPUKernel = require('./kernel')
var CPUFunctionBuilder = require('./function-builder')
module.exports = (function (_RunnerBase) {
_inherits(CPURunner, _RunnerBase)
function CPURunner(settings) {
_classCallCheck(this, CPURunner)
var _this = _possibleConstructorReturn(
this,
(
CPURunner.__proto__ ||
Object.getPrototypeOf(CPURunner)
).call(this, new CPUFunctionBuilder(), settings),
)
_this.Kernel = CPUKernel
_this.kernel = null
return _this
}
_createClass(CPURunner, [
{
key: 'getMode',
value: function getMode() {
return 'cpu'
},
},
])
return CPURunner
})(RunnerBase)
},
{
'../../core/utils': 25,
'../runner-base': 10,
'./function-builder': 1,
'./kernel': 4,
},
],
6: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
module.exports = (function () {
function FunctionBuilderBase(gpu) {
_classCallCheck(this, FunctionBuilderBase)
this.nodeMap = {}
this.nativeFunctions = {}
this.gpu = gpu
this.rootKernel = null
this.Node = null
}
_createClass(FunctionBuilderBase, [
{
key: 'addNativeFunction',
value: function addNativeFunction(
functionName,
glslFunctionString,
) {
this.nativeFunctions[functionName] =
glslFunctionString
},
},
{
key: 'addFunction',
value: function addFunction(
functionName,
jsFunction,
options,
paramTypes,
returnType,
) {
this.addFunctionNode(
new this.Node(
functionName,
jsFunction,
options,
paramTypes,
returnType,
).setAddFunction(
this.addFunction.bind(this),
),
)
},
},
{
key: 'addFunctions',
value: function addFunctions(functions, options) {
if (functions) {
if (Array.isArray(functions)) {
for (
var i = 0;
i < functions.length;
i++
) {
this.addFunction(
null,
functions[i],
options,
)
}
} else {
for (var p in functions) {
this.addFunction(
p,
functions[p],
options,
)
}
}
}
},
},
{
key: 'addNativeFunctions',
value: function addNativeFunctions(
nativeFunctions,
) {
for (var functionName in nativeFunctions) {
if (
!nativeFunctions.hasOwnProperty(
functionName,
)
)
continue
this.addNativeFunction(
functionName,
nativeFunctions[functionName],
)
}
},
},
{
key: 'addFunctionNode',
value: function addFunctionNode(inNode) {
this.nodeMap[inNode.functionName] = inNode
if (inNode.isRootKernel) {
this.rootKernel = inNode
}
},
},
{
key: 'traceFunctionCalls',
value: function traceFunctionCalls(
functionName,
retList,
parent,
) {
functionName = functionName || 'kernel'
retList = retList || []
var fNode = this.nodeMap[functionName]
if (fNode) {
var functionIndex =
retList.indexOf(functionName)
if (functionIndex === -1) {
retList.push(functionName)
if (parent) {
fNode.parent = parent
}
fNode.getFunctionString()
for (
var i = 0;
i < fNode.calledFunctions.length;
++i
) {
this.traceFunctionCalls(
fNode.calledFunctions[i],
retList,
fNode,
)
}
} else {
var dependantFunctionName =
retList.splice(functionIndex, 1)[0]
retList.push(dependantFunctionName)
}
}
if (this.nativeFunctions[functionName]) {
if (retList.indexOf(functionName) >= 0) {
} else {
retList.push(functionName)
}
}
return retList
},
},
{
key: 'addKernel',
value: function addKernel(
fnString,
options,
paramNames,
paramTypes,
) {
var kernelNode = new this.Node(
'kernel',
fnString,
options,
paramTypes,
)
kernelNode.setAddFunction(
this.addFunction.bind(this),
)
kernelNode.paramNames = paramNames
kernelNode.paramTypes = paramTypes
kernelNode.isRootKernel = true
this.addFunctionNode(kernelNode)
return kernelNode
},
},
{
key: 'addSubKernel',
value: function addSubKernel(
jsFunction,
options,
paramTypes,
returnType,
) {
var kernelNode = new this.Node(
null,
jsFunction,
options,
paramTypes,
returnType,
)
kernelNode.setAddFunction(
this.addFunction.bind(this),
)
kernelNode.isSubKernel = true
this.addFunctionNode(kernelNode)
return kernelNode
},
},
{
key: 'getPrototypeString',
value: function getPrototypeString(functionName) {
return this.getPrototypes(functionName).join(
'\n',
)
},
},
{
key: 'getPrototypes',
value: function getPrototypes(functionName) {
this.rootKernel.generate()
if (functionName) {
return this.getPrototypesFromFunctionNames(
this.traceFunctionCalls(
functionName,
[],
).reverse(),
)
}
return this.getPrototypesFromFunctionNames(
Object.keys(this.nodeMap),
)
},
},
{
key: 'getStringFromFunctionNames',
value: function getStringFromFunctionNames(
functionList,
) {
var ret = []
for (var i = 0; i < functionList.length; ++i) {
var node = this.nodeMap[functionList[i]]
if (node) {
ret.push(
this.nodeMap[
functionList[i]
].getFunctionString(),
)
}
}
return ret.join('\n')
},
},
{
key: 'getPrototypesFromFunctionNames',
value: function getPrototypesFromFunctionNames(
functionList,
opt,
) {
var ret = []
for (var i = 0; i < functionList.length; ++i) {
var functionName = functionList[i]
var node = this.nodeMap[functionName]
if (node) {
ret.push(
node.getFunctionPrototypeString(
opt,
),
)
} else if (
this.nativeFunctions[functionName]
) {
ret.push(
this.nativeFunctions[functionName],
)
}
}
return ret
},
},
{
key: 'getPrototypeStringFromFunctionNames',
value: function getPrototypeStringFromFunctionNames(
functionList,
opt,
) {
return this.getPrototypesFromFunctionNames(
functionList,
opt,
).toString()
},
},
{
key: 'getString',
value: function getString(functionName, opt) {
if (opt === undefined) {
opt = {}
}
if (functionName) {
return this.getStringFromFunctionNames(
this.traceFunctionCalls(
functionName,
[],
opt,
).reverse(),
opt,
)
}
return this.getStringFromFunctionNames(
Object.keys(this.nodeMap),
opt,
)
},
},
{
key: 'polyfillStandardFunctions',
value: function polyfillStandardFunctions() {
throw new Error(
'polyfillStandardFunctions not defined on base function builder',
)
},
},
])
return FunctionBuilderBase
})()
},
{},
],
7: [
function (require, module, exports) {
'use strict'
var _typeof =
typeof Symbol === 'function' &&
typeof Symbol.iterator === 'symbol'
? function (obj) {
return typeof obj
}
: function (obj) {
return obj &&
typeof Symbol === 'function' &&
obj.constructor === Symbol &&
obj !== Symbol.prototype
? 'symbol'
: typeof obj
}
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
var utils = require('../core/utils')
var acorn = require('acorn')
module.exports = (function () {
function BaseFunctionNode(
functionName,
jsFunction,
options,
paramTypes,
returnType,
) {
_classCallCheck(this, BaseFunctionNode)
this.calledFunctions = []
this.calledFunctionsArguments = {}
this.initVariables = []
this.readVariables = []
this.writeVariables = []
this.addFunction = null
this.isRootKernel = false
this.isSubKernel = false
this.parent = null
this.debug = null
this.prototypeOnly = null
this.constants = null
this.output = null
if (options) {
if (options.hasOwnProperty('debug')) {
this.debug = options.debug
}
if (options.hasOwnProperty('prototypeOnly')) {
this.prototypeOnly = options.prototypeOnly
}
if (options.hasOwnProperty('constants')) {
this.constants = options.constants
}
if (options.hasOwnProperty('output')) {
this.output = options.output
}
if (options.hasOwnProperty('loopMaxIterations')) {
this.loopMaxIterations =
options.loopMaxIterations
}
}
if (!jsFunction) {
throw 'jsFunction, parameter is missing'
}
this.jsFunctionString = jsFunction.toString()
if (!utils.isFunctionString(this.jsFunctionString)) {
console.error(
'jsFunction, to string conversion check failed: not a function?',
this.jsFunctionString,
)
throw 'jsFunction, to string conversion check failed: not a function?'
}
if (!utils.isFunction(jsFunction)) {
this.jsFunction = null
} else {
this.jsFunction = jsFunction
}
this.functionName =
functionName ||
(jsFunction && jsFunction.name) ||
utils.getFunctionNameFromString(
this.jsFunctionString,
)
if (!this.functionName) {
throw 'jsFunction, missing name argument or value'
}
this.paramNames = utils.getParamNamesFromString(
this.jsFunctionString,
)
if (paramTypes) {
if (Array.isArray(paramTypes)) {
if (
paramTypes.length !== this.paramNames.length
) {
throw (
'Invalid argument type array length, against function length -> (' +
paramTypes.length +
',' +
this.paramNames.length +
')'
)
}
this.paramTypes = paramTypes
} else if (
(typeof paramTypes === 'undefined'
? 'undefined'
: _typeof(paramTypes)) === 'object'
) {
var paramVariableNames = Object.keys(paramTypes)
if (paramTypes.hasOwnProperty('returns')) {
this.returnType = paramTypes.returns
paramVariableNames.splice(
paramVariableNames.indexOf('returns'),
1,
)
}
if (
paramVariableNames.length > 0 &&
paramVariableNames.length !==
this.paramNames.length
) {
throw (
'Invalid argument type array length, against function length -> (' +
paramVariableNames.length +
',' +
this.paramNames.length +
')'
)
} else {
this.paramTypes = this.paramNames.map(
function (key) {
if (
paramTypes.hasOwnProperty(key)
) {
return paramTypes[key]
} else {
return 'float'
}
},
)
}
}
} else {
this.paramTypes = []
}
if (!this.returnType) {
this.returnType = returnType || 'float'
}
}
_createClass(BaseFunctionNode, [
{
key: 'isIdentifierConstant',
value: function isIdentifierConstant(paramName) {
if (!this.constants) return false
return this.constants.hasOwnProperty(paramName)
},
},
{
key: 'setAddFunction',
value: function setAddFunction(fn) {
this.addFunction = fn
return this
},
},
{
key: 'getJsFunction',
value: function getJsFunction() {
if (this.jsFunction) {
return this.jsFunction
}
if (this.jsFunctionString) {
this.jsFunction = eval(
this.jsFunctionString,
)
return this.jsFunction
}
throw 'Missing jsFunction, and jsFunctionString parameter'
},
},
{
key: 'astMemberExpressionUnroll',
value: function astMemberExpressionUnroll(
ast,
funcParam,
) {
if (ast.type === 'Identifier') {
return ast.name
} else if (ast.type === 'ThisExpression') {
return 'this'
}
if (ast.type === 'MemberExpression') {
if (ast.object && ast.property) {
if (
ast.object.hasOwnProperty('name') &&
ast.object.name[0] === '_'
) {
return this.astMemberExpressionUnroll(
ast.property,
funcParam,
)
}
return (
this.astMemberExpressionUnroll(
ast.object,
funcParam,
) +
'.' +
this.astMemberExpressionUnroll(
ast.property,
funcParam,
)
)
}
}
if (ast.hasOwnProperty('expressions')) {
var firstExpression = ast.expressions[0]
if (
firstExpression.type === 'Literal' &&
firstExpression.value === 0 &&
ast.expressions.length === 2
) {
return this.astMemberExpressionUnroll(
ast.expressions[1],
)
}
}
throw this.astErrorOutput(
'Unknown CallExpression_unroll',
ast,
funcParam,
)
},
},
{
key: 'getJsAST',
value: function getJsAST(inParser) {
if (this.jsFunctionAST) {
return this.jsFunctionAST
}
inParser = inParser || acorn
if (inParser === null) {
throw 'Missing JS to AST parser'
}
var ast = inParser.parse(
'var ' +
this.functionName +
' = ' +
this.jsFunctionString +
';',
{
locations: true,
},
)
if (ast === null) {
throw 'Failed to parse JS code'
}
var funcAST = ast.body[0].declarations[0].init
this.jsFunctionAST = funcAST
return funcAST
},
},
{
key: 'getFunctionString',
value: function getFunctionString() {
this.generate()
return this.functionString
},
},
{
key: 'setFunctionString',
value: function setFunctionString(functionString) {
this.functionString = functionString
},
},
{
key: 'getParamType',
value: function getParamType(paramName) {
var paramIndex =
this.paramNames.indexOf(paramName)
if (paramIndex === -1) return null
if (!this.parent) return null
if (this.paramTypes[paramIndex])
return this.paramTypes[paramIndex]
var calledFunctionArguments =
this.parent.calledFunctionsArguments[
this.functionName
]
for (
var i = 0;
i < calledFunctionArguments.length;
i++
) {
var calledFunctionArgument =
calledFunctionArguments[i]
if (
calledFunctionArgument[paramIndex] !==
null
) {
return (this.paramTypes[paramIndex] =
calledFunctionArgument[
paramIndex
].type)
}
}
return null
},
},
{
key: 'getUserParamName',
value: function getUserParamName(paramName) {
var paramIndex =
this.paramNames.indexOf(paramName)
if (paramIndex === -1) return null
if (!this.parent) return null
var calledFunctionArguments =
this.parent.calledFunctionsArguments[
this.functionName
]
for (
var i = 0;
i < calledFunctionArguments.length;
i++
) {
var calledFunctionArgument =
calledFunctionArguments[i]
if (
calledFunctionArgument[paramIndex] !==
null
) {
return calledFunctionArgument[
paramIndex
].name
}
}
return null
},
},
{
key: 'generate',
value: function generate(options) {
throw new Error(
'generate not defined on BaseFunctionNode',
)
},
},
{
key: 'astErrorOutput',
value: function astErrorOutput(
error,
ast,
funcParam,
) {
console.error(
utils.getAstString(
this.jsFunctionString,
ast,
),
)
console.error(error, ast, funcParam)
return error
},
},
{
key: 'astDebuggerStatement',
value: function astDebuggerStatement(
arrNode,
retArr,
funcParam,
) {
return retArr
},
},
])
return BaseFunctionNode
})()
},
{ '../core/utils': 25, acorn: 27 },
],
8: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
var utils = require('../core/utils')
module.exports = (function () {
function BaseKernel(fnString, settings) {
_classCallCheck(this, BaseKernel)
this.paramNames =
utils.getParamNamesFromString(fnString)
this.fnString = fnString
this.output = null
this.debug = false
this.graphical = false
this.loopMaxIterations = 0
this.constants = null
this.wraparound = null
this.hardcodeConstants = null
this.outputToTexture = null
this.texSize = null
this._canvas = null
this._webGl = null
this.threadDim = null
this.floatTextures = null
this.floatOutput = null
this.floatOutputForce = null
this.addFunction = null
this.functions = null
this.nativeFunctions = null
this.copyData = true
this.subKernels = null
this.subKernelProperties = null
this.subKernelNames = null
this.subKernelOutputVariableNames = null
this.functionBuilder = null
this.paramTypes = null
for (var p in settings) {
if (
!settings.hasOwnProperty(p) ||
!this.hasOwnProperty(p)
)
continue
this[p] = settings[p]
}
if (settings.hasOwnProperty('canvas')) {
this._canvas = settings.canvas
}
if (settings.hasOwnProperty('output')) {
this.setOutput(settings.output)
}
if (!this._canvas) this._canvas = utils.initCanvas()
}
_createClass(BaseKernel, [
{
key: 'build',
value: function build() {
throw new Error('"build" not defined on Base')
},
},
{
key: 'setupParams',
value: function setupParams(args) {
var paramTypes = (this.paramTypes = [])
for (var i = 0; i < args.length; i++) {
var param = args[i]
var paramType = utils.getArgumentType(param)
paramTypes.push(paramType)
}
},
},
{
key: 'setAddFunction',
value: function setAddFunction(cb) {
this.addFunction = cb
return this
},
},
{
key: 'setFunctions',
value: function setFunctions(functions) {
this.functions = functions
return this
},
},
{
key: 'setOutput',
value: function setOutput(output) {
if (output.hasOwnProperty('x')) {
if (output.hasOwnProperty('y')) {
if (output.hasOwnProperty('z')) {
this.output = [
output.x,
output.y,
output.z,
]
} else {
this.output = [output.x, output.y]
}
} else {
this.output = [output.x]
}
} else {
this.output = output
}
return this
},
},
{
key: 'setDebug',
value: function setDebug(flag) {
this.debug = flag
return this
},
},
{
key: 'setGraphical',
value: function setGraphical(flag) {
this.graphical = flag
return this
},
},
{
key: 'setLoopMaxIterations',
value: function setLoopMaxIterations(max) {
this.loopMaxIterations = max
return this
},
},
{
key: 'setConstants',
value: function setConstants(constants) {
this.constants = constants
return this
},
},
{
key: 'setWraparound',
value: function setWraparound(flag) {
console.warn(
'Wraparound mode is not supported and undocumented.',
)
this.wraparound = flag
return this
},
},
{
key: 'setHardcodeConstants',
value: function setHardcodeConstants(flag) {
this.hardcodeConstants = flag
return this
},
},
{
key: 'setOutputToTexture',
value: function setOutputToTexture(flag) {
this.outputToTexture = flag
return this
},
},
{
key: 'setFloatTextures',
value: function setFloatTextures(flag) {
this.floatTextures = flag
return this
},
},
{
key: 'setFloatOutput',
value: function setFloatOutput(flag) {
this.floatOutput = flag
return this
},
},
{
key: 'setFloatOutputForce',
value: function setFloatOutputForce(flag) {
this.floatOutputForce = flag
return this
},
},
{
key: 'setCanvas',
value: function setCanvas(canvas) {
this._canvas = canvas
return this
},
},
{
key: 'setWebGl',
value: function setWebGl(webGl) {
this._webGl = webGl
return this
},
},
{
key: 'setCopyData',
value: function setCopyData(copyData) {
this.copyData = copyData
return this
},
},
{
key: 'getCanvas',
value: function getCanvas() {
return this._canvas
},
},
{
key: 'getWebGl',
value: function getWebGl() {
return this._webGl
},
},
{
key: 'validateOptions',
value: function validateOptions() {
throw new Error('validateOptions not defined')
},
},
{
key: 'exec',
value: function exec() {
return this.execute.apply(this, arguments)
},
},
{
key: 'execute',
value: function execute() {
var _this = this
var args =
arguments.length === 1
? [arguments[0]]
: Array.apply(null, arguments)
return utils.newPromise(function (
accept,
reject,
) {
try {
accept(_this.run.apply(_this, args))
} catch (e) {
reject(e)
}
})
},
},
{
key: 'addSubKernel',
value: function addSubKernel(fnString) {
if (this.subKernels === null) {
this.subKernels = []
this.subKernelNames = []
}
this.subKernels.push(fnString)
this.subKernelNames.push(
utils.getFunctionNameFromString(fnString),
)
return this
},
},
{
key: 'addSubKernelProperty',
value: function addSubKernelProperty(
property,
fnString,
) {
if (this.subKernelProperties === null) {
this.subKernelProperties = {}
this.subKernelNames = []
}
if (
this.subKernelProperties.hasOwnProperty(
property,
)
) {
throw new Error(
'cannot add sub kernel ' +
property +
', already defined',
)
}
this.subKernelProperties[property] = fnString
this.subKernelNames.push(
utils.getFunctionNameFromString(fnString),
)
return this
},
},
{
key: 'addNativeFunction',
value: function addNativeFunction(name, source) {
this.functionBuilder.addNativeFunction(
name,
source,
)
},
},
])
return BaseKernel
})()
},
{ '../core/utils': 25 },
],
9: [
function (require, module, exports) {
'use strict'
var utils = require('../core/utils')
module.exports = function kernelRunShortcut(kernel) {
var shortcut = function shortcut() {
return kernel.run.apply(kernel, arguments)
}
utils.allPropertiesOf(kernel).forEach(function (key) {
if (key[0] === '_' && key[1] === '_') return
if (typeof kernel[key] === 'function') {
if (
key.substring(0, 3) === 'add' ||
key.substring(0, 3) === 'set'
) {
shortcut[key] = function () {
kernel[key].apply(kernel, arguments)
return shortcut
}
} else {
shortcut[key] = kernel[key].bind(kernel)
}
} else {
shortcut.__defineGetter__(key, function () {
return kernel[key]
})
shortcut.__defineSetter__(key, function (value) {
kernel[key] = value
})
}
})
shortcut.kernel = kernel
return shortcut
}
},
{ '../core/utils': 25 },
],
10: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
var utils = require('../core/utils')
var kernelRunShortcut = require('./kernel-run-shortcut')
module.exports = (function () {
function BaseRunner(functionBuilder, settings) {
_classCallCheck(this, BaseRunner)
settings = settings || {}
this.kernel = settings.kernel
this.canvas = settings.canvas
this.webGl = settings.webGl
this.fn = null
this.functionBuilder = functionBuilder
this.fnString = null
this.endianness = utils.systemEndianness()
this.functionBuilder.polyfillStandardFunctions()
}
_createClass(BaseRunner, [
{
key: 'textureToArray',
value: function textureToArray(texture) {
var copy = this.createKernel(function (x) {
return x[
this.thread.z
][this.thread.y][this.thread.x]
})
return copy(texture)
},
},
{
key: 'deleteTexture',
value: function deleteTexture(texture) {
this.webGl.deleteTexture(texture.texture)
},
},
{
key: 'buildPromiseKernel',
value: function buildPromiseKernel() {
throw new Error('not yet implemented')
},
},
{
key: 'getMode',
value: function getMode() {
throw new Error(
'"mode" not implemented on BaseRunner',
)
},
},
{
key: 'buildKernel',
value: function buildKernel(fn, settings) {
settings = Object.assign({}, settings || {})
var fnString = fn.toString()
if (!settings.functionBuilder) {
settings.functionBuilder =
this.functionBuilder
}
if (!settings.canvas) {
settings.canvas = this.canvas
}
if (!settings.webGl) {
settings.webGl = this.webgl
}
return kernelRunShortcut(
new this.Kernel(fnString, settings),
)
},
},
])
return BaseRunner
})()
},
{ '../core/utils': 25, './kernel-run-shortcut': 9 },
],
11: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called",
)
}
return call &&
(typeof call === 'object' || typeof call === 'function')
? call
: self
}
function _inherits(subClass, superClass) {
if (
typeof superClass !== 'function' &&
superClass !== null
) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass,
)
}
subClass.prototype = Object.create(
superClass && superClass.prototype,
{
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true,
},
},
)
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass)
}
var FunctionBuilderBase = require('../function-builder-base')
var WebGLFunctionNode = require('./function-node')
module.exports = (function (_FunctionBuilderBase) {
_inherits(WebGLFunctionBuilder, _FunctionBuilderBase)
function WebGLFunctionBuilder() {
_classCallCheck(this, WebGLFunctionBuilder)
var _this = _possibleConstructorReturn(
this,
(
WebGLFunctionBuilder.__proto__ ||
Object.getPrototypeOf(WebGLFunctionBuilder)
).call(this),
)
_this.Node = WebGLFunctionNode
return _this
}
_createClass(
WebGLFunctionBuilder,
[
{
key: 'polyfillStandardFunctions',
value: function polyfillStandardFunctions() {
this.addFunction('round', _round)
},
},
],
[
{
key: 'round',
value: function round(a) {
return _round(a)
},
},
],
)
return WebGLFunctionBuilder
})(FunctionBuilderBase)
function _round(a) {
return Math.floor(a + 0.5)
}
},
{ '../function-builder-base': 6, './function-node': 12 },
],
12: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called",
)
}
return call &&
(typeof call === 'object' || typeof call === 'function')
? call
: self
}
function _inherits(subClass, superClass) {
if (
typeof superClass !== 'function' &&
superClass !== null
) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass,
)
}
subClass.prototype = Object.create(
superClass && superClass.prototype,
{
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true,
},
},
)
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass)
}
var FunctionNodeBase = require('../function-node-base')
var utils = require('../../core/utils')
var jsMathPrefix = 'Math.'
var localPrefix = 'this.'
var constantsPrefix = 'this.constants.'
var DECODE32_ENCODE32 = /decode32\(\s+encode32\(/g
var ENCODE32_DECODE32 = /encode32\(\s+decode32\(/g
module.exports = (function (_FunctionNodeBase) {
_inherits(WebGLFunctionNode, _FunctionNodeBase)
function WebGLFunctionNode() {
_classCallCheck(this, WebGLFunctionNode)
return _possibleConstructorReturn(
this,
(
WebGLFunctionNode.__proto__ ||
Object.getPrototypeOf(WebGLFunctionNode)
).apply(this, arguments),
)
}
_createClass(
WebGLFunctionNode,
[
{
key: 'generate',
value: function generate() {
if (this.debug) {
console.log(this)
}
if (this.prototypeOnly) {
return WebGLFunctionNode.astFunctionPrototype(
this.getJsAST(),
[],
this,
)
.join('')
.trim()
} else {
this.functionStringArray =
this.astGeneric(
this.getJsAST(),
[],
this,
)
}
this.functionString = webGlRegexOptimize(
this.functionStringArray
.join('')
.trim(),
)
return this.functionString
},
},
{
key: 'astGeneric',
value: function astGeneric(
ast,
retArr,
funcParam,
) {
if (ast === null) {
throw this.astErrorOutput(
'NULL ast',
ast,
funcParam,
)
} else {
if (Array.isArray(ast)) {
for (
var i = 0;
i < ast.length;
i++
) {
this.astGeneric(
ast[i],
retArr,
funcParam,
)
}
return retArr
}
switch (ast.type) {
case 'FunctionDeclaration':
return this.astFunctionDeclaration(
ast,
retArr,
funcParam,
)
case 'FunctionExpression':
return this.astFunctionExpression(
ast,
retArr,
funcParam,
)
case 'ReturnStatement':
return this.astReturnStatement(
ast,
retArr,
funcParam,
)
case 'Literal':
return this.astLiteral(
ast,
retArr,
funcParam,
)
case 'BinaryExpression':
return this.astBinaryExpression(
ast,
retArr,
funcParam,
)
case 'Identifier':
return this.astIdentifierExpression(
ast,
retArr,
funcParam,
)
case 'AssignmentExpression':
return this.astAssignmentExpression(
ast,
retArr,
funcParam,
)
case 'ExpressionStatement':
return this.astExpressionStatement(
ast,
retArr,
funcParam,
)
case 'EmptyStatement':
return this.astEmptyStatement(
ast,
retArr,
funcParam,
)
case 'BlockStatement':
return this.astBlockStatement(
ast,
retArr,
funcParam,
)
case 'IfStatement':
return this.astIfStatement(
ast,
retArr,
funcParam,
)
case 'BreakStatement':
return this.astBreakStatement(
ast,
retArr,
funcParam,
)
case 'ContinueStatement':
return this.astContinueStatement(
ast,
retArr,
funcParam,
)
case 'ForStatement':
return this.astForStatement(
ast,
retArr,
funcParam,
)
case 'WhileStatement':
return this.astWhileStatement(
ast,
retArr,
funcParam,
)
case 'VariableDeclaration':
return this.astVariableDeclaration(
ast,
retArr,
funcParam,
)
case 'VariableDeclarator':
return this.astVariableDeclarator(
ast,
retArr,
funcParam,
)
case 'ThisExpression':
return this.astThisExpression(
ast,
retArr,
funcParam,
)
case 'SequenceExpression':
return this.astSequenceExpression(
ast,
retArr,
funcParam,
)
case 'UnaryExpression':
return this.astUnaryExpression(
ast,
retArr,
funcParam,
)
case 'UpdateExpression':
return this.astUpdateExpression(
ast,
retArr,
funcParam,
)
case 'LogicalExpression':
return this.astLogicalExpression(
ast,
retArr,
funcParam,
)
case 'MemberExpression':
return this.astMemberExpression(
ast,
retArr,
funcParam,
)
case 'CallExpression':
return this.astCallExpression(
ast,
retArr,
funcParam,
)
case 'ArrayExpression':
return this.astArrayExpression(
ast,
retArr,
funcParam,
)
case 'DebuggerStatement':
return this.astDebuggerStatement(
ast,
retArr,
funcParam,
)
}
throw this.astErrorOutput(
'Unknown ast type : ' + ast.type,
ast,
funcParam,
)
}
},
},
{
key: 'astFunctionDeclaration',
value: function astFunctionDeclaration(
ast,
retArr,
funcParam,
) {
if (this.addFunction) {
this.addFunction(
null,
utils.getAstString(
this.jsFunctionString,
ast,
),
)
}
return retArr
},
},
{
key: 'astFunctionExpression',
value: function astFunctionExpression(
ast,
retArr,
funcParam,
) {
if (funcParam.isRootKernel) {
retArr.push('void')
funcParam.kernalAst = ast
} else {
retArr.push(funcParam.returnType)
}
retArr.push(' ')
retArr.push(funcParam.functionName)
retArr.push('(')
if (!funcParam.isRootKernel) {
for (
var i = 0;
i < funcParam.paramNames.length;
++i
) {
var paramName =
funcParam.paramNames[i]
if (i > 0) {
retArr.push(', ')
}
var type =
funcParam.getParamType(
paramName,
)
switch (type) {
case 'Texture':
case 'Input':
case 'Array':
retArr.push('sampler2D')
break
default:
retArr.push('float')
}
retArr.push(' ')
retArr.push('user_')
retArr.push(paramName)
}
}
retArr.push(') {\n')
for (
var _i = 0;
_i < ast.body.body.length;
++_i
) {
this.astGeneric(
ast.body.body[_i],
retArr,
funcParam,
)
retArr.push('\n')
}
retArr.push('}\n')
return retArr
},
},
{
key: 'astReturnStatement',
value: function astReturnStatement(
ast,
retArr,
funcParam,
) {
if (funcParam.isRootKernel) {
retArr.push('kernelResult = ')
this.astGeneric(
ast.argument,
retArr,
funcParam,
)
retArr.push(';')
retArr.push('return;')
} else if (funcParam.isSubKernel) {
retArr.push(
funcParam.functionName +
'Result = ',
)
this.astGeneric(
ast.argument,
retArr,
funcParam,
)
retArr.push(';')
retArr.push(
'return ' +
funcParam.functionName +
'Result;',
)
} else {
retArr.push('return ')
this.astGeneric(
ast.argument,
retArr,
funcParam,
)
retArr.push(';')
}
return retArr
},
},
{
key: 'astLiteral',
value: function astLiteral(
ast,
retArr,
funcParam,
) {
if (isNaN(ast.value)) {
throw this.astErrorOutput(
'Non-numeric literal not supported : ' +
ast.value,
ast,
funcParam,
)
}
retArr.push(ast.value)
if (Number.isInteger(ast.value)) {
retArr.push('.0')
}
return retArr
},
},
{
key: 'astBinaryExpression',
value: function astBinaryExpression(
ast,
retArr,
funcParam,
) {
retArr.push('(')
if (ast.operator === '%') {
retArr.push('mod(')
this.astGeneric(
ast.left,
retArr,
funcParam,
)
retArr.push(',')
this.astGeneric(
ast.right,
retArr,
funcParam,
)
retArr.push(')')
} else if (ast.operator === '===') {
this.astGeneric(
ast.left,
retArr,
funcParam,
)
retArr.push('==')
this.astGeneric(
ast.right,
retArr,
funcParam,
)
} else if (ast.operator === '!==') {
this.astGeneric(
ast.left,
retArr,
funcParam,
)
retArr.push('!=')
this.astGeneric(
ast.right,
retArr,
funcParam,
)
} else {
this.astGeneric(
ast.left,
retArr,
funcParam,
)
retArr.push(ast.operator)
this.astGeneric(
ast.right,
retArr,
funcParam,
)
}
retArr.push(')')
return retArr
},
},
{
key: 'astIdentifierExpression',
value: function astIdentifierExpression(
idtNode,
retArr,
funcParam,
) {
if (idtNode.type !== 'Identifier') {
throw this.astErrorOutput(
'IdentifierExpression - not an Identifier',
idtNode,
funcParam,
)
}
switch (idtNode.name) {
case 'gpu_threadX':
retArr.push('threadId.x')
break
case 'gpu_threadY':
retArr.push('threadId.y')
break
case 'gpu_threadZ':
retArr.push('threadId.z')
break
case 'gpu_outputX':
retArr.push('uOutputDim.x')
break
case 'gpu_outputY':
retArr.push('uOutputDim.y')
break
case 'gpu_outputZ':
retArr.push('uOutputDim.z')
break
default:
if (
this.constants &&
this.constants.hasOwnProperty(
idtNode.name,
)
) {
retArr.push(
'constants_' + idtNode.name,
)
} else {
var userParamName =
funcParam.getUserParamName(
idtNode.name,
)
if (userParamName !== null) {
retArr.push(
'user_' + userParamName,
)
} else {
retArr.push(
'user_' + idtNode.name,
)
}
}
}
return retArr
},
},
{
key: 'astForStatement',
value: function astForStatement(
forNode,
retArr,
funcParam,
) {
if (forNode.type !== 'ForStatement') {
throw this.astErrorOutput(
'Invalid for statment',
forNode,
funcParam,
)
}
if (
forNode.test &&
forNode.test.type === 'BinaryExpression'
) {
if (
forNode.test.right.type ===
'Identifier' &&
forNode.test.operator === '<' &&
this.isIdentifierConstant(
forNode.test.right.name,
) === false
) {
if (!this.loopMaxIterations) {
console.warn(
'Warning: loopMaxIterations is not set! Using default of 1000 which may result in unintended behavior.',
)
console.warn(
'Set loopMaxIterations or use a for loop of fixed length to silence this message.',
)
}
retArr.push('for (')
this.astGeneric(
forNode.init,
retArr,
funcParam,
)
this.astGeneric(
forNode.test.left,
retArr,
funcParam,
)
retArr.push(forNode.test.operator)
retArr.push('LOOP_MAX')
retArr.push(';')
this.astGeneric(
forNode.update,
retArr,
funcParam,
)
retArr.push(')')
retArr.push('{\n')
retArr.push('if (')
this.astGeneric(
forNode.test.left,
retArr,
funcParam,
)
retArr.push(forNode.test.operator)
this.astGeneric(
forNode.test.right,
retArr,
funcParam,
)
retArr.push(') {\n')
if (
forNode.body.type ===
'BlockStatement'
) {
for (
var i = 0;
i <
forNode.body.body.length;
i++
) {
this.astGeneric(
forNode.body.body[i],
retArr,
funcParam,
)
}
} else {
this.astGeneric(
forNode.body,
retArr,
funcParam,
)
}
retArr.push('} else {\n')
retArr.push('break;\n')
retArr.push('}\n')
retArr.push('}\n')
return retArr
} else {
var declarations = JSON.parse(
JSON.stringify(
forNode.init.declarations,
),
)
var updateArgument =
forNode.update.argument
if (
!Array.isArray(declarations) ||
declarations.length < 1
) {
console.log(
this.jsFunctionString,
)
throw new Error(
'Error: Incompatible for loop declaration',
)
}
if (declarations.length > 1) {
var initArgument = null
for (
var _i2 = 0;
_i2 < declarations.length;
_i2++
) {
var declaration =
declarations[_i2]
if (
declaration.id.name ===
updateArgument.name
) {
initArgument =
declaration
declarations.splice(
_i2,
1,
)
} else {
retArr.push('float ')
this.astGeneric(
declaration,
retArr,
funcParam,
)
retArr.push(';')
}
}
retArr.push('for (float ')
this.astGeneric(
initArgument,
retArr,
funcParam,
)
retArr.push(';')
} else {
retArr.push('for (')
this.astGeneric(
forNode.init,
retArr,
funcParam,
)
}
this.astGeneric(
forNode.test,
retArr,
funcParam,
)
retArr.push(';')
this.astGeneric(
forNode.update,
retArr,
funcParam,
)
retArr.push(')')
this.astGeneric(
forNode.body,
retArr,
funcParam,
)
return retArr
}
}
throw this.astErrorOutput(
'Invalid for statement',
forNode,
funcParam,
)
},
},
{
key: 'astWhileStatement',
value: function astWhileStatement(
whileNode,
retArr,
funcParam,
) {
if (whileNode.type !== 'WhileStatement') {
throw this.astErrorOutput(
'Invalid while statment',
whileNode,
funcParam,
)
}
retArr.push(
'for (float i = 0.0; i < LOOP_MAX; i++) {',
)
retArr.push('if (')
this.astGeneric(
whileNode.test,
retArr,
funcParam,
)
retArr.push(') {\n')
this.astGeneric(
whileNode.body,
retArr,
funcParam,
)
retArr.push('} else {\n')
retArr.push('break;\n')
retArr.push('}\n')
retArr.push('}\n')
return retArr
},
},
{
key: 'astAssignmentExpression',
value: function astAssignmentExpression(
assNode,
retArr,
funcParam,
) {
if (assNode.operator === '%=') {
this.astGeneric(
assNode.left,
retArr,
funcParam,
)
retArr.push('=')
retArr.push('mod(')
this.astGeneric(
assNode.left,
retArr,
funcParam,
)
retArr.push(',')
this.astGeneric(
assNode.right,
retArr,
funcParam,
)
retArr.push(')')
} else {
this.astGeneric(
assNode.left,
retArr,
funcParam,
)
retArr.push(assNode.operator)
this.astGeneric(
assNode.right,
retArr,
funcParam,
)
return retArr
}
},
},
{
key: 'astEmptyStatement',
value: function astEmptyStatement(
eNode,
retArr,
funcParam,
) {
return retArr
},
},
{
key: 'astBlockStatement',
value: function astBlockStatement(
bNode,
retArr,
funcParam,
) {
retArr.push('{\n')
for (
var i = 0;
i < bNode.body.length;
i++
) {
this.astGeneric(
bNode.body[i],
retArr,
funcParam,
)
}
retArr.push('}\n')
return retArr
},
},
{
key: 'astExpressionStatement',
value: function astExpressionStatement(
esNode,
retArr,
funcParam,
) {
this.astGeneric(
esNode.expression,
retArr,
funcParam,
)
retArr.push(';\n')
return retArr
},
},
{
key: 'astVariableDeclaration',
value: function astVariableDeclaration(
vardecNode,
retArr,
funcParam,
) {
retArr.push('float ')
for (
var i = 0;
i < vardecNode.declarations.length;
i++
) {
if (i > 0) {
retArr.push(',')
}
this.astGeneric(
vardecNode.declarations[i],
retArr,
funcParam,
)
}
retArr.push(';')
return retArr
},
},
{
key: 'astVariableDeclarator',
value: function astVariableDeclarator(
ivardecNode,
retArr,
funcParam,
) {
this.astGeneric(
ivardecNode.id,
retArr,
funcParam,
)
if (ivardecNode.init !== null) {
retArr.push('=')
this.astGeneric(
ivardecNode.init,
retArr,
funcParam,
)
}
return retArr
},
},
{
key: 'astIfStatement',
value: function astIfStatement(
ifNode,
retArr,
funcParam,
) {
retArr.push('if (')
this.astGeneric(
ifNode.test,
retArr,
funcParam,
)
retArr.push(')')
if (
ifNode.consequent.type ===
'BlockStatement'
) {
this.astGeneric(
ifNode.consequent,
retArr,
funcParam,
)
} else {
retArr.push(' {\n')
this.astGeneric(
ifNode.consequent,
retArr,
funcParam,
)
retArr.push('\n}\n')
}
if (ifNode.alternate) {
retArr.push('else ')
if (
ifNode.alternate.type ===
'BlockStatement'
) {
this.astGeneric(
ifNode.alternate,
retArr,
funcParam,
)
} else {
retArr.push(' {\n')
this.astGeneric(
ifNode.alternate,
retArr,
funcParam,
)
retArr.push('\n}\n')
}
}
return retArr
},
},
{
key: 'astBreakStatement',
value: function astBreakStatement(
brNode,
retArr,
funcParam,
) {
retArr.push('break;\n')
return retArr
},
},
{
key: 'astContinueStatement',
value: function astContinueStatement(
crNode,
retArr,
funcParam,
) {
retArr.push('continue;\n')
return retArr
},
},
{
key: 'astLogicalExpression',
value: function astLogicalExpression(
logNode,
retArr,
funcParam,
) {
retArr.push('(')
this.astGeneric(
logNode.left,
retArr,
funcParam,
)
retArr.push(logNode.operator)
this.astGeneric(
logNode.right,
retArr,
funcParam,
)
retArr.push(')')
return retArr
},
},
{
key: 'astUpdateExpression',
value: function astUpdateExpression(
uNode,
retArr,
funcParam,
) {
if (uNode.prefix) {
retArr.push(uNode.operator)
this.astGeneric(
uNode.argument,
retArr,
funcParam,
)
} else {
this.astGeneric(
uNode.argument,
retArr,
funcParam,
)
retArr.push(uNode.operator)
}
return retArr
},
},
{
key: 'astUnaryExpression',
value: function astUnaryExpression(
uNode,
retArr,
funcParam,
) {
if (uNode.prefix) {
retArr.push(uNode.operator)
this.astGeneric(
uNode.argument,
retArr,
funcParam,
)
} else {
this.astGeneric(
uNode.argument,
retArr,
funcParam,
)
retArr.push(uNode.operator)
}
return retArr
},
},
{
key: 'astThisExpression',
value: function astThisExpression(
tNode,
retArr,
funcParam,
) {
retArr.push('this')
return retArr
},
},
{
key: 'astMemberExpression',
value: function astMemberExpression(
mNode,
retArr,
funcParam,
) {
if (mNode.computed) {
if (
mNode.object.type === 'Identifier'
) {
var reqName = mNode.object.name
var funcName =
funcParam.functionName ||
'kernel'
var assumeNotTexture = false
if (funcParam.paramNames) {
var idx =
funcParam.paramNames.indexOf(
reqName,
)
if (
idx >= 0 &&
funcParam.paramTypes[
idx
] === 'float'
) {
assumeNotTexture = true
}
}
if (assumeNotTexture) {
this.astGeneric(
mNode.object,
retArr,
funcParam,
)
retArr.push('[int(')
this.astGeneric(
mNode.property,
retArr,
funcParam,
)
retArr.push(')]')
} else {
retArr.push('get(')
this.astGeneric(
mNode.object,
retArr,
funcParam,
)
retArr.push(', vec2(')
this.astGeneric(
mNode.object,
retArr,
funcParam,
)
retArr.push('Size[0],')
this.astGeneric(
mNode.object,
retArr,
funcParam,
)
retArr.push('Size[1]), vec3(')
this.astGeneric(
mNode.object,
retArr,
funcParam,
)
retArr.push('Dim[0],')
this.astGeneric(
mNode.object,
retArr,
funcParam,
)
retArr.push('Dim[1],')
this.astGeneric(
mNode.object,
retArr,
funcParam,
)
retArr.push('Dim[2]')
retArr.push('), ')
this.astGeneric(
mNode.property,
retArr,
funcParam,
)
retArr.push(')')
}
} else {
this.astGeneric(
mNode.object,
retArr,
funcParam,
)
var last = retArr.pop()
retArr.push(',')
this.astGeneric(
mNode.property,
retArr,
funcParam,
)
retArr.push(last)
}
} else {
var unrolled =
this.astMemberExpressionUnroll(
mNode,
)
var unrolled_lc = unrolled.toLowerCase()
if (
unrolled.indexOf(
constantsPrefix,
) === 0
) {
unrolled =
'constants_' +
unrolled.slice(
constantsPrefix.length,
)
}
switch (unrolled_lc) {
case 'this.thread.x':
retArr.push('threadId.x')
break
case 'this.thread.y':
retArr.push('threadId.y')
break
case 'this.thread.z':
retArr.push('threadId.z')
break
case 'this.output.x':
retArr.push(
this.output[0] + '.0',
)
break
case 'this.output.y':
retArr.push(
this.output[1] + '.0',
)
break
case 'this.output.z':
retArr.push(
this.output[2] + '.0',
)
break
default:
retArr.push(unrolled)
}
}
return retArr
},
},
{
key: 'astSequenceExpression',
value: function astSequenceExpression(
sNode,
retArr,
funcParam,
) {
for (
var i = 0;
i < sNode.expressions.length;
i++
) {
if (i > 0) {
retArr.push(',')
}
this.astGeneric(
sNode.expressions,
retArr,
funcParam,
)
}
return retArr
},
},
{
key: 'astCallExpression',
value: function astCallExpression(
ast,
retArr,
funcParam,
) {
if (ast.callee) {
var funcName =
this.astMemberExpressionUnroll(
ast.callee,
)
if (
funcName.indexOf(jsMathPrefix) === 0
) {
funcName = funcName.slice(
jsMathPrefix.length,
)
}
if (
funcName.indexOf(localPrefix) === 0
) {
funcName = funcName.slice(
localPrefix.length,
)
}
if (
funcParam.calledFunctions.indexOf(
funcName,
) < 0
) {
funcParam.calledFunctions.push(
funcName,
)
}
if (
!funcParam.hasOwnProperty(
'funcName',
)
) {
funcParam.calledFunctionsArguments[
funcName
] = []
}
var functionArguments = []
funcParam.calledFunctionsArguments[
funcName
].push(functionArguments)
retArr.push(funcName)
retArr.push('(')
for (
var i = 0;
i < ast.arguments.length;
++i
) {
var argument = ast.arguments[i]
if (i > 0) {
retArr.push(', ')
}
this.astGeneric(
argument,
retArr,
funcParam,
)
if (
argument.type === 'Identifier'
) {
var paramIndex =
funcParam.paramNames.indexOf(
argument.name,
)
if (paramIndex === -1) {
functionArguments.push(null)
} else {
functionArguments.push({
name: argument.name,
type: funcParam
.paramTypes[
paramIndex
],
})
}
} else {
functionArguments.push(null)
}
}
retArr.push(')')
return retArr
}
throw this.astErrorOutput(
'Unknown CallExpression',
ast,
funcParam,
)
return retArr
},
},
{
key: 'astArrayExpression',
value: function astArrayExpression(
arrNode,
retArr,
funcParam,
) {
var arrLen = arrNode.elements.length
retArr.push('float[' + arrLen + '](')
for (var i = 0; i < arrLen; ++i) {
if (i > 0) {
retArr.push(', ')
}
var subNode = arrNode.elements[i]
this.astGeneric(
subNode,
retArr,
funcParam,
)
}
retArr.push(')')
return retArr
},
},
{
key: 'getFunctionPrototypeString',
value: function getFunctionPrototypeString() {
if (this.webGlFunctionPrototypeString) {
return this.webGlFunctionPrototypeString
}
return (this.webGlFunctionPrototypeString =
this.generate())
},
},
{
key: 'build',
value: function build() {
return (
this.getFunctionPrototypeString()
.length > 0
)
},
},
],
[
{
key: 'astFunctionPrototype',
value: function astFunctionPrototype(
ast,
retArr,
funcParam,
) {
if (
funcParam.isRootKernel ||
funcParam.isSubKernel
) {
return retArr
}
retArr.push(funcParam.returnType)
retArr.push(' ')
retArr.push(funcParam.functionName)
retArr.push('(')
for (
var i = 0;
i < funcParam.paramNames.length;
++i
) {
if (i > 0) {
retArr.push(', ')
}
retArr.push(funcParam.paramTypes[i])
retArr.push(' ')
retArr.push('user_')
retArr.push(funcParam.paramNames[i])
}
retArr.push(');\n')
return retArr
},
},
],
)
return WebGLFunctionNode
})(FunctionNodeBase)
function isIdentifierKernelParam(paramName, ast, funcParam) {
return funcParam.paramNames.indexOf(paramName) !== -1
}
function ensureIndentifierType(
paramName,
expectedType,
ast,
funcParam,
) {
var start = ast.loc.start
if (
!isIdentifierKernelParam(paramName, funcParam) &&
expectedType !== 'float'
) {
throw new Error(
'Error unexpected identifier ' +
paramName +
' on line ' +
start.line,
)
} else {
var actualType =
funcParam.paramTypes[
funcParam.paramNames.indexOf(paramName)
]
if (actualType !== expectedType) {
throw new Error(
'Error unexpected identifier ' +
paramName +
' on line ' +
start.line,
)
}
}
}
function webGlRegexOptimize(inStr) {
return inStr
.replace(DECODE32_ENCODE32, '((')
.replace(ENCODE32_DECODE32, '((')
}
},
{ '../../core/utils': 25, '../function-node-base': 7 },
],
13: [
function (require, module, exports) {
'use strict'
var utils = require('../../core/utils')
var kernelRunShortcut = require('../kernel-run-shortcut')
module.exports = function (gpuKernel, name) {
return (
'() => {\n ' +
kernelRunShortcut.toString() +
';\n const utils = {\n allPropertiesOf: function ' +
utils.allPropertiesOf.toString() +
',\n clone: function ' +
utils.clone.toString() +
',\n splitArray: function ' +
utils.splitArray.toString() +
',\n getArgumentType: function ' +
utils.getArgumentType.toString() +
',\n getDimensions: function ' +
utils.getDimensions.toString() +
',\n dimToTexSize: function ' +
utils.dimToTexSize.toString() +
',\n copyFlatten: function ' +
utils.copyFlatten.toString() +
',\n flatten: function ' +
utils.flatten.toString() +
",\n systemEndianness: '" +
utils.systemEndianness() +
"',\n initWebGl: function " +
utils.initWebGl.toString() +
',\n isArray: function ' +
utils.isArray.toString() +
'\n };\n class ' +
(name || 'Kernel') +
' {\n constructor() {\n this.argumentsLength = 0;\n this._canvas = null;\n this._webGl = null;\n this.built = false;\n this.program = null;\n this.paramNames = ' +
JSON.stringify(gpuKernel.paramNames) +
';\n this.paramTypes = ' +
JSON.stringify(gpuKernel.paramTypes) +
';\n this.texSize = ' +
JSON.stringify(gpuKernel.texSize) +
';\n this.output = ' +
JSON.stringify(gpuKernel.output) +
';\n this.compiledFragShaderString = `' +
gpuKernel.compiledFragShaderString +
'`;\n\t\t this.compiledVertShaderString = `' +
gpuKernel.compiledVertShaderString +
'`;\n\t\t this.programUniformLocationCache = {};\n\t\t this.textureCache = {};\n\t\t this.subKernelOutputTextures = null;\n }\n ' +
gpuKernel._getFragShaderString.toString() +
'\n ' +
gpuKernel._getVertShaderString.toString() +
'\n validateOptions() {}\n setupParams() {}\n setCanvas(canvas) { this._canvas = canvas; return this; }\n setWebGl(webGl) { this._webGl = webGl; return this; }\n ' +
gpuKernel.getUniformLocation.toString() +
'\n ' +
gpuKernel.setupParams.toString() +
'\n ' +
gpuKernel.build.toString() +
'\n\t\t ' +
gpuKernel.run.toString() +
'\n\t\t ' +
gpuKernel._addArgument.toString() +
'\n\t\t ' +
gpuKernel.getArgumentTexture.toString() +
'\n\t\t ' +
gpuKernel.getTextureCache.toString() +
'\n\t\t ' +
gpuKernel.getOutputTexture.toString() +
'\n\t\t ' +
gpuKernel.renderOutput.toString() +
'\n };\n return kernelRunShortcut(new Kernel());\n };'
)
}
},
{ '../../core/utils': 25, '../kernel-run-shortcut': 9 },
],
14: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called",
)
}
return call &&
(typeof call === 'object' || typeof call === 'function')
? call
: self
}
function _inherits(subClass, superClass) {
if (
typeof superClass !== 'function' &&
superClass !== null
) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass,
)
}
subClass.prototype = Object.create(
superClass && superClass.prototype,
{
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true,
},
},
)
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass)
}
var KernelBase = require('../kernel-base')
var utils = require('../../core/utils')
var Texture = require('../../core/texture')
var fragShaderString = require('./shader-frag')
var vertShaderString = require('./shader-vert')
var kernelString = require('./kernel-string')
var canvases = []
var maxTexSizes = {}
module.exports = (function (_KernelBase) {
_inherits(WebGLKernel, _KernelBase)
function WebGLKernel(fnString, settings) {
_classCallCheck(this, WebGLKernel)
var _this = _possibleConstructorReturn(
this,
(
WebGLKernel.__proto__ ||
Object.getPrototypeOf(WebGLKernel)
).call(this, fnString, settings),
)
_this.textureCache = {}
_this.threadDim = {}
_this.programUniformLocationCache = {}
_this.framebuffer = null
_this.buffer = null
_this.program = null
_this.outputToTexture = settings.outputToTexture
_this.endianness = utils.systemEndianness()
_this.subKernelOutputTextures = null
_this.subKernelOutputVariableNames = null
_this.argumentsLength = 0
_this.ext = null
_this.compiledFragShaderString = null
_this.compiledVertShaderString = null
_this.extDrawBuffersMap = null
_this.outputTexture = null
_this.maxTexSize = null
if (!_this._webGl)
_this._webGl = utils.initWebGl(_this.getCanvas())
return _this
}
_createClass(WebGLKernel, [
{
key: 'validateOptions',
value: function validateOptions() {
var isFloatReadPixel =
utils.isFloatReadPixelsSupported()
if (
this.floatTextures === true &&
!utils.OES_texture_float
) {
throw new Error(
'Float textures are not supported on this browser',
)
} else if (
this.floatOutput === true &&
this.floatOutputForce !== true &&
!isFloatReadPixel
) {
throw new Error(
'Float texture outputs are not supported on this browser',
)
} else if (
this.floatTextures === undefined &&
utils.OES_texture_float
) {
this.floatTextures = true
this.floatOutput = isFloatReadPixel
}
if (!this.output || this.output.length === 0) {
if (arguments.length !== 1) {
throw new Error(
'Auto output only supported for kernels with only one input',
)
}
var argType = utils.getArgumentType(
arguments[0],
)
if (argType === 'Array') {
this.output =
utils.getDimensions(argType)
} else if (argType === 'Texture') {
this.output = arguments[0].output
} else {
throw new Error(
'Auto output not supported for input type: ' +
argType,
)
}
}
this.texSize = utils.dimToTexSize(
{
floatTextures: this.floatTextures,
floatOutput: this.floatOutput,
},
this.output,
true,
)
if (this.graphical) {
if (this.output.length !== 2) {
throw new Error(
'Output must have 2 dimensions on graphical mode',
)
}
if (this.floatOutput) {
this.floatOutput = false
console.warn(
'Cannot use graphical mode and float output at the same time',
)
}
this.texSize = utils.clone(this.output)
} else if (
this.floatOutput === undefined &&
utils.OES_texture_float
) {
this.floatOutput = true
}
},
},
{
key: 'updateMaxTexSize',
value: function updateMaxTexSize() {
var texSize = this.texSize
var canvas = this._canvas
if (this.maxTexSize === null) {
var canvasIndex = canvases.indexOf(canvas)
if (canvasIndex === -1) {
canvasIndex = canvases.length
canvases.push(canvas)
maxTexSizes[canvasIndex] = [
texSize[0],
texSize[1],
]
}
this.maxTexSize = maxTexSizes[canvasIndex]
}
if (this.maxTexSize[0] < texSize[0]) {
this.maxTexSize[0] = texSize[0]
}
if (this.maxTexSize[1] < texSize[1]) {
this.maxTexSize[1] = texSize[1]
}
},
},
{
key: 'build',
value: function build() {
this.validateOptions()
this.setupParams(arguments)
this.updateMaxTexSize()
var texSize = this.texSize
var gl = this._webGl
var canvas = this._canvas
gl.enable(gl.SCISSOR_TEST)
gl.viewport(
0,
0,
this.maxTexSize[0],
this.maxTexSize[1],
)
canvas.width = this.maxTexSize[0]
canvas.height = this.maxTexSize[1]
var threadDim = (this.threadDim = utils.clone(
this.output,
))
while (threadDim.length < 3) {
threadDim.push(1)
}
if (this.functionBuilder) this._addKernels()
var compiledVertShaderString =
this._getVertShaderString(arguments)
var vertShader = gl.createShader(
gl.VERTEX_SHADER,
)
gl.shaderSource(
vertShader,
compiledVertShaderString,
)
gl.compileShader(vertShader)
var compiledFragShaderString =
this._getFragShaderString(arguments)
var fragShader = gl.createShader(
gl.FRAGMENT_SHADER,
)
gl.shaderSource(
fragShader,
compiledFragShaderString,
)
gl.compileShader(fragShader)
if (
!gl.getShaderParameter(
vertShader,
gl.COMPILE_STATUS,
)
) {
console.log(compiledVertShaderString)
console.error(
'An error occurred compiling the shaders: ' +
gl.getShaderInfoLog(vertShader),
)
throw new Error(
'Error compiling vertex shader',
)
}
if (
!gl.getShaderParameter(
fragShader,
gl.COMPILE_STATUS,
)
) {
console.log(compiledFragShaderString)
console.error(
'An error occurred compiling the shaders: ' +
gl.getShaderInfoLog(fragShader),
)
throw new Error(
'Error compiling fragment shader',
)
}
if (this.debug) {
console.log('Options:')
console.dir(this)
console.log('GLSL Shader Output:')
console.log(compiledFragShaderString)
}
var program = (this.program =
gl.createProgram())
gl.attachShader(program, vertShader)
gl.attachShader(program, fragShader)
gl.linkProgram(program)
this.framebuffer = gl.createFramebuffer()
this.framebuffer.width = texSize[0]
this.framebuffer.height = texSize[1]
var vertices = new Float32Array([
-1, -1, 1, -1, -1, 1, 1, 1,
])
var texCoords = new Float32Array([
0, 0, 1, 0, 0, 1, 1, 1,
])
var texCoordOffset = vertices.byteLength
var buffer = this.buffer
if (!buffer) {
buffer = this.buffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
gl.bufferData(
gl.ARRAY_BUFFER,
vertices.byteLength +
texCoords.byteLength,
gl.STATIC_DRAW,
)
} else {
gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
}
gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices)
gl.bufferSubData(
gl.ARRAY_BUFFER,
texCoordOffset,
texCoords,
)
var aPosLoc = gl.getAttribLocation(
this.program,
'aPos',
)
gl.enableVertexAttribArray(aPosLoc)
gl.vertexAttribPointer(
aPosLoc,
2,
gl.FLOAT,
gl.FALSE,
0,
0,
)
var aTexCoordLoc = gl.getAttribLocation(
this.program,
'aTexCoord',
)
gl.enableVertexAttribArray(aTexCoordLoc)
gl.vertexAttribPointer(
aTexCoordLoc,
2,
gl.FLOAT,
gl.FALSE,
0,
texCoordOffset,
)
this.setupOutputTexture()
if (this.subKernelOutputTextures !== null) {
var extDrawBuffersMap =
(this.extDrawBuffersMap = [
gl.COLOR_ATTACHMENT0,
])
for (
var i = 0;
i < this.subKernelOutputTextures.length;
i++
) {
var subKernelOutputTexture =
this.subKernelOutputTextures[i]
extDrawBuffersMap.push(
gl.COLOR_ATTACHMENT0 + i + 1,
)
gl.activeTexture(
gl.TEXTURE0 + arguments.length + i,
)
gl.bindTexture(
gl.TEXTURE_2D,
subKernelOutputTexture,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_WRAP_S,
gl.CLAMP_TO_EDGE,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_WRAP_T,
gl.CLAMP_TO_EDGE,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MIN_FILTER,
gl.NEAREST,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MAG_FILTER,
gl.NEAREST,
)
if (this.floatOutput) {
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
texSize[0],
texSize[1],
0,
gl.RGBA,
gl.FLOAT,
null,
)
} else {
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
texSize[0],
texSize[1],
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
null,
)
}
}
}
},
},
{
key: 'run',
value: function run() {
if (this.program === null) {
this.build.apply(this, arguments)
}
var paramNames = this.paramNames
var paramTypes = this.paramTypes
var texSize = this.texSize
var gl = this._webGl
gl.useProgram(this.program)
gl.scissor(0, 0, texSize[0], texSize[1])
if (!this.hardcodeConstants) {
var uOutputDimLoc =
this.getUniformLocation('uOutputDim')
gl.uniform3fv(uOutputDimLoc, this.threadDim)
var uTexSizeLoc =
this.getUniformLocation('uTexSize')
gl.uniform2fv(uTexSizeLoc, texSize)
}
var ratioLoc = this.getUniformLocation('ratio')
gl.uniform2f(
ratioLoc,
texSize[0] / this.maxTexSize[0],
texSize[1] / this.maxTexSize[1],
)
this.argumentsLength = 0
for (
var texIndex = 0;
texIndex < paramNames.length;
texIndex++
) {
this._addArgument(
arguments[texIndex],
paramTypes[texIndex],
paramNames[texIndex],
)
}
if (this.graphical) {
gl.bindRenderbuffer(gl.RENDERBUFFER, null)
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4)
return
}
gl.bindFramebuffer(
gl.FRAMEBUFFER,
this.framebuffer,
)
var outputTexture = this.outputTexture
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
outputTexture,
0,
)
if (this.subKernelOutputTextures !== null) {
for (
var i = 0;
i < this.subKernelOutputTextures.length;
i++
) {
var subKernelOutputTexture =
this.subKernelOutputTextures[i]
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0 + i + 1,
gl.TEXTURE_2D,
subKernelOutputTexture,
0,
)
}
this.ext.drawBuffersWEBGL(
this.extDrawBuffersMap,
)
}
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4)
if (this.subKernelOutputTextures !== null) {
if (this.subKernels !== null) {
var output = []
output.result =
this.renderOutput(outputTexture)
for (
var _i = 0;
_i < this.subKernels.length;
_i++
) {
output.push(
new Texture(
this.subKernelOutputTextures[
_i
],
texSize,
this.output,
this._webGl,
),
)
}
return output
} else if (
this.subKernelProperties !== null
) {
var _output = {
result: this.renderOutput(
outputTexture,
),
}
var _i2 = 0
for (var p in this
.subKernelProperties) {
if (
!this.subKernelProperties.hasOwnProperty(
p,
)
)
continue
_output[p] = new Texture(
this.subKernelOutputTextures[
_i2
],
texSize,
this.output,
this._webGl,
)
_i2++
}
return _output
}
}
return this.renderOutput(outputTexture)
},
},
{
key: 'renderOutput',
value: function renderOutput(outputTexture) {
var texSize = this.texSize
var gl = this._webGl
var threadDim = this.threadDim
var output = this.output
if (this.outputToTexture) {
return new Texture(
outputTexture,
texSize,
output,
this._webGl,
)
} else {
var result = void 0
if (this.floatOutput) {
result = new Float32Array(
texSize[0] * texSize[1] * 4,
)
gl.readPixels(
0,
0,
texSize[0],
texSize[1],
gl.RGBA,
gl.FLOAT,
result,
)
} else {
var bytes = new Uint8Array(
texSize[0] * texSize[1] * 4,
)
gl.readPixels(
0,
0,
texSize[0],
texSize[1],
gl.RGBA,
gl.UNSIGNED_BYTE,
bytes,
)
result = new Float32Array(bytes.buffer)
}
result = result.subarray(
0,
threadDim[0] *
threadDim[1] *
threadDim[2],
)
if (output.length === 1) {
return result
} else if (output.length === 2) {
return utils.splitArray(
result,
output[0],
)
} else if (output.length === 3) {
var cube = utils.splitArray(
result,
output[0] * output[1],
)
return cube.map(function (x) {
return utils.splitArray(
x,
output[0],
)
})
}
}
},
},
{
key: 'getOutputTexture',
value: function getOutputTexture() {
return this.getTextureCache('OUTPUT')
},
},
{
key: 'detachOutputTexture',
value: function detachOutputTexture() {
this.detachTextureCache('OUTPUT')
},
},
{
key: 'setupOutputTexture',
value: function setupOutputTexture() {
var gl = this._webGl
var texSize = this.texSize
this.detachOutputTexture()
this.outputTexture = this.getOutputTexture()
gl.activeTexture(
gl.TEXTURE0 + this.paramNames.length,
)
gl.bindTexture(
gl.TEXTURE_2D,
this.outputTexture,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_WRAP_S,
gl.CLAMP_TO_EDGE,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_WRAP_T,
gl.CLAMP_TO_EDGE,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MIN_FILTER,
gl.NEAREST,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MAG_FILTER,
gl.NEAREST,
)
if (this.floatOutput) {
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
texSize[0],
texSize[1],
0,
gl.RGBA,
gl.FLOAT,
null,
)
} else {
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
texSize[0],
texSize[1],
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
null,
)
}
},
},
{
key: 'getArgumentTexture',
value: function getArgumentTexture(name) {
return this.getTextureCache('ARGUMENT_' + name)
},
},
{
key: 'getSubKernelTexture',
value: function getSubKernelTexture(name) {
return this.getTextureCache(
'SUB_KERNEL_' + name,
)
},
},
{
key: 'getTextureCache',
value: function getTextureCache(name) {
if (this.outputToTexture) {
return this._webGl.createTexture()
}
if (this.textureCache.hasOwnProperty(name)) {
return this.textureCache[name]
}
return (this.textureCache[name] =
this._webGl.createTexture())
},
},
{
key: 'detachTextureCache',
value: function detachTextureCache(name) {
delete this.textureCache[name]
},
},
{
key: 'getUniformLocation',
value: function getUniformLocation(name) {
var location =
this.programUniformLocationCache[name]
if (!location) {
location = this._webGl.getUniformLocation(
this.program,
name,
)
this.programUniformLocationCache[name] =
location
}
return location
},
},
{
key: '_getFragShaderArtifactMap',
value: function _getFragShaderArtifactMap(args) {
return {
HEADER: this._getHeaderString(),
LOOP_MAX: this._getLoopMaxString(),
CONSTANTS: this._getConstantsString(),
DECODE32_ENDIANNESS:
this._getDecode32EndiannessString(),
ENCODE32_ENDIANNESS:
this._getEncode32EndiannessString(),
GET_WRAPAROUND:
this._getGetWraparoundString(),
GET_TEXTURE_CHANNEL:
this._getGetTextureChannelString(),
GET_TEXTURE_INDEX:
this._getGetTextureIndexString(),
GET_RESULT: this._getGetResultString(),
MAIN_PARAMS:
this._getMainParamsString(args),
MAIN_CONSTANTS:
this._getMainConstantsString(),
KERNEL: this._getKernelString(),
MAIN_RESULT: this._getMainResultString(),
}
},
},
{
key: '_addArgument',
value: function _addArgument(value, type, name) {
var gl = this._webGl
var argumentTexture =
this.getArgumentTexture(name)
if (value instanceof Texture) {
type = 'Texture'
}
switch (type) {
case 'Array': {
var dim = utils.getDimensions(
value,
true,
)
var size = utils.dimToTexSize(
{
floatTextures:
this.floatTextures,
floatOutput: this.floatOutput,
},
dim,
)
gl.activeTexture(
gl.TEXTURE0 + this.argumentsLength,
)
gl.bindTexture(
gl.TEXTURE_2D,
argumentTexture,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_WRAP_S,
gl.CLAMP_TO_EDGE,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_WRAP_T,
gl.CLAMP_TO_EDGE,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MIN_FILTER,
gl.NEAREST,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MAG_FILTER,
gl.NEAREST,
)
var length = size[0] * size[1]
if (this.floatTextures) {
length *= 4
}
var valuesFlat = new Float32Array(
length,
)
utils.flattenTo(value, valuesFlat)
var buffer = void 0
if (this.floatTextures) {
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
size[0],
size[1],
0,
gl.RGBA,
gl.FLOAT,
valuesFlat,
)
} else {
buffer = new Uint8Array(
valuesFlat.buffer,
)
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
size[0],
size[1],
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
buffer,
)
}
var loc = this.getUniformLocation(
'user_' + name,
)
var locSize = this.getUniformLocation(
'user_' + name + 'Size',
)
var dimLoc = this.getUniformLocation(
'user_' + name + 'Dim',
)
if (!this.hardcodeConstants) {
gl.uniform3fv(dimLoc, dim)
gl.uniform2fv(locSize, size)
}
gl.uniform1i(loc, this.argumentsLength)
break
}
case 'Number': {
var _loc = this.getUniformLocation(
'user_' + name,
)
gl.uniform1f(_loc, value)
break
}
case 'Input': {
var input = value
var _dim = input.size
var _size = utils.dimToTexSize(
{
floatTextures:
this.floatTextures,
floatOutput: this.floatOutput,
},
_dim,
)
gl.activeTexture(
gl.TEXTURE0 + this.argumentsLength,
)
gl.bindTexture(
gl.TEXTURE_2D,
argumentTexture,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_WRAP_S,
gl.CLAMP_TO_EDGE,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_WRAP_T,
gl.CLAMP_TO_EDGE,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MIN_FILTER,
gl.NEAREST,
)
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MAG_FILTER,
gl.NEAREST,
)
var _length = _size[0] * _size[1]
var inputArray = void 0
if (this.floatTextures) {
_length *= 4
inputArray = new Float32Array(
_length,
)
inputArray.set(input.value)
} else {
inputArray = input.value
}
if (this.floatTextures) {
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
_size[0],
_size[1],
0,
gl.RGBA,
gl.FLOAT,
inputArray,
)
} else {
var _buffer = new Uint8Array(
inputArray.buffer,
)
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
_size[0],
_size[1],
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
_buffer,
)
}
var _loc2 = this.getUniformLocation(
'user_' + name,
)
var _locSize = this.getUniformLocation(
'user_' + name + 'Size',
)
var _dimLoc = this.getUniformLocation(
'user_' + name + 'Dim',
)
if (!this.hardcodeConstants) {
gl.uniform3fv(_dimLoc, _dim)
gl.uniform2fv(_locSize, _size)
}
gl.uniform1i(
_loc2,
this.argumentsLength,
)
break
}
case 'Texture': {
var inputTexture = value
var _dim2 = utils.getDimensions(
inputTexture,
true,
)
var _size2 = inputTexture.size
if (
inputTexture.texture ===
this.outputTexture
) {
this.setupOutputTexture()
}
gl.activeTexture(
gl.TEXTURE0 + this.argumentsLength,
)
gl.bindTexture(
gl.TEXTURE_2D,
inputTexture.texture,
)
var _loc3 = this.getUniformLocation(
'user_' + name,
)
var _locSize2 = this.getUniformLocation(
'user_' + name + 'Size',
)
var _dimLoc2 = this.getUniformLocation(
'user_' + name + 'Dim',
)
gl.uniform3fv(_dimLoc2, _dim2)
gl.uniform2fv(_locSize2, _size2)
gl.uniform1i(
_loc3,
this.argumentsLength,
)
break
}
default:
throw new Error(
'Input type not supported (WebGL): ' +
value,
)
}
this.argumentsLength++
},
},
{
key: '_getHeaderString',
value: function _getHeaderString() {
return this.subKernels !== null ||
this.subKernelProperties !== null
? '#extension GL_EXT_draw_buffers : require\n'
: ''
},
},
{
key: '_getLoopMaxString',
value: function _getLoopMaxString() {
return this.loopMaxIterations
? ' ' +
parseInt(this.loopMaxIterations) +
'.0;\n'
: ' 1000.0;\n'
},
},
{
key: '_getConstantsString',
value: function _getConstantsString() {
var result = []
var threadDim = this.threadDim
var texSize = this.texSize
if (this.hardcodeConstants) {
result.push(
'highp vec3 uOutputDim = vec3(' +
threadDim[0] +
',' +
threadDim[1] +
', ' +
threadDim[2] +
')',
'highp vec2 uTexSize = vec2(' +
texSize[0] +
', ' +
texSize[1] +
')',
)
} else {
result.push(
'uniform highp vec3 uOutputDim',
'uniform highp vec2 uTexSize',
)
}
return this._linesToString(result)
},
},
{
key: '_getTextureCoordinate',
value: function _getTextureCoordinate() {
var names = this.subKernelOutputVariableNames
if (names === null || names.length < 1) {
return 'varying highp vec2 vTexCoord;\n'
} else {
return 'out highp vec2 vTexCoord;\n'
}
},
},
{
key: '_getDecode32EndiannessString',
value: function _getDecode32EndiannessString() {
return this.endianness === 'LE'
? ''
: ' rgba.rgba = rgba.abgr;\n'
},
},
{
key: '_getEncode32EndiannessString',
value: function _getEncode32EndiannessString() {
return this.endianness === 'LE'
? ''
: ' rgba.rgba = rgba.abgr;\n'
},
},
{
key: '_getGetWraparoundString',
value: function _getGetWraparoundString() {
return this.wraparound
? ' xyz = mod(xyz, texDim);\n'
: ''
},
},
{
key: '_getGetTextureChannelString',
value: function _getGetTextureChannelString() {
if (!this.floatTextures) return ''
return this._linesToString([
' int channel = int(integerMod(index, 4.0))',
' index = float(int(index) / 4)',
])
},
},
{
key: '_getGetTextureIndexString',
value: function _getGetTextureIndexString() {
return this.floatTextures
? ' index = float(int(index)/4);\n'
: ''
},
},
{
key: '_getGetResultString',
value: function _getGetResultString() {
if (!this.floatTextures)
return ' return decode32(texel);\n'
return this._linesToString([
' if (channel == 0) return texel.r',
' if (channel == 1) return texel.g',
' if (channel == 2) return texel.b',
' if (channel == 3) return texel.a',
])
},
},
{
key: '_getMainParamsString',
value: function _getMainParamsString(args) {
var result = []
var paramTypes = this.paramTypes
var paramNames = this.paramNames
for (var i = 0; i < paramNames.length; i++) {
var param = args[i]
var paramName = paramNames[i]
var paramType = paramTypes[i]
if (this.hardcodeConstants) {
if (
paramType === 'Array' ||
paramType === 'Texture'
) {
var paramDim = utils.getDimensions(
param,
true,
)
var paramSize = utils.dimToTexSize(
{
floatTextures:
this.floatTextures,
floatOutput:
this.floatOutput,
},
paramDim,
)
result.push(
'uniform highp sampler2D user_' +
paramName,
'highp vec2 user_' +
paramName +
'Size = vec2(' +
paramSize[0] +
'.0, ' +
paramSize[1] +
'.0)',
'highp vec3 user_' +
paramName +
'Dim = vec3(' +
paramDim[0] +
'.0, ' +
paramDim[1] +
'.0, ' +
paramDim[2] +
'.0)',
)
} else if (
paramType === 'Number' &&
Number.isInteger(param)
) {
result.push(
'highp float user_' +
paramName +
' = ' +
param +
'.0',
)
} else if (paramType === 'Number') {
result.push(
'highp float user_' +
paramName +
' = ' +
param,
)
}
} else {
if (
paramType === 'Array' ||
paramType === 'Texture' ||
paramType === 'Input'
) {
result.push(
'uniform highp sampler2D user_' +
paramName,
'uniform highp vec2 user_' +
paramName +
'Size',
'uniform highp vec3 user_' +
paramName +
'Dim',
)
} else if (paramType === 'Number') {
result.push(
'uniform highp float user_' +
paramName,
)
}
}
}
return this._linesToString(result)
},
},
{
key: '_getMainConstantsString',
value: function _getMainConstantsString() {
var result = []
if (this.constants) {
for (var name in this.constants) {
if (
!this.constants.hasOwnProperty(name)
)
continue
var value = parseFloat(
this.constants[name],
)
if (Number.isInteger(value)) {
result.push(
'const float constants_' +
name +
' = ' +
parseInt(value) +
'.0',
)
} else {
result.push(
'const float constants_' +
name +
' = ' +
parseFloat(value),
)
}
}
}
return this._linesToString(result)
},
},
{
key: '_getKernelString',
value: function _getKernelString() {
var result = []
var names = this.subKernelOutputVariableNames
if (names !== null) {
result.push(
'highp float kernelResult = 0.0',
)
for (var i = 0; i < names.length; i++) {
result.push(
'highp float ' +
names[i] +
' = 0.0',
)
}
} else {
result.push(
'highp float kernelResult = 0.0',
)
}
return (
this._linesToString(result) +
this.functionBuilder.getPrototypeString(
'kernel',
)
)
},
},
{
key: '_getMainResultString',
value: function _getMainResultString() {
var names = this.subKernelOutputVariableNames
var result = []
if (this.floatOutput) {
result.push(' index *= 4.0')
}
if (this.graphical) {
result.push(
' threadId = indexTo3D(index, uOutputDim)',
' kernel()',
' gl_FragColor = actualColor',
)
} else if (this.floatOutput) {
var channels = ['r', 'g', 'b', 'a']
for (var i = 0; i < channels.length; ++i) {
result.push(
' threadId = indexTo3D(index, uOutputDim)',
)
result.push(' kernel()')
if (names) {
result.push(
' gl_FragData[0].' +
channels[i] +
' = kernelResult',
)
for (
var j = 0;
j < names.length;
++j
) {
result.push(
' gl_FragData[' +
(j + 1) +
'].' +
channels[i] +
' = ' +
names[j],
)
}
} else {
result.push(
' gl_FragColor.' +
channels[i] +
' = kernelResult',
)
}
if (i < channels.length - 1) {
result.push(' index += 1.0')
}
}
} else if (names !== null) {
result.push(
' threadId = indexTo3D(index, uOutputDim)',
)
result.push(' kernel()')
result.push(
' gl_FragData[0] = encode32(kernelResult)',
)
for (
var _i3 = 0;
_i3 < names.length;
_i3++
) {
result.push(
' gl_FragData[' +
(_i3 + 1) +
'] = encode32(' +
names[_i3] +
')',
)
}
} else {
result.push(
' threadId = indexTo3D(index, uOutputDim)',
' kernel()',
' gl_FragColor = encode32(kernelResult)',
)
}
return this._linesToString(result)
},
},
{
key: '_linesToString',
value: function _linesToString(lines) {
if (lines.length > 0) {
return lines.join(';\n') + ';\n'
} else {
return '\n'
}
},
},
{
key: '_replaceArtifacts',
value: function _replaceArtifacts(src, map) {
return src.replace(
/[ ]*__([A-Z]+[0-9]*([_]?[A-Z])*)__;\n/g,
function (match, artifact) {
if (map.hasOwnProperty(artifact)) {
return map[artifact]
}
throw 'unhandled artifact ' + artifact
},
)
},
},
{
key: '_addKernels',
value: function _addKernels() {
var builder = this.functionBuilder
var gl = this._webGl
builder.addFunctions(this.functions, {
constants: this.constants,
output: this.output,
})
builder.addNativeFunctions(this.nativeFunctions)
builder.addKernel(
this.fnString,
{
prototypeOnly: false,
constants: this.constants,
output: this.output,
debug: this.debug,
loopMaxIterations:
this.loopMaxIterations,
},
this.paramNames,
this.paramTypes,
)
if (this.subKernels !== null) {
var ext = (this.ext =
gl.getExtension('WEBGL_draw_buffers'))
if (!ext)
throw new Error(
'could not instantiate draw buffers extension',
)
this.subKernelOutputTextures = []
this.subKernelOutputVariableNames = []
for (
var i = 0;
i < this.subKernels.length;
i++
) {
var subKernel = this.subKernels[i]
builder.addSubKernel(subKernel, {
prototypeOnly: false,
constants: this.constants,
output: this.output,
debug: this.debug,
loopMaxIterations:
this.loopMaxIterations,
})
this.subKernelOutputTextures.push(
this.getSubKernelTexture(i),
)
this.subKernelOutputVariableNames.push(
subKernel.name + 'Result',
)
}
} else if (this.subKernelProperties !== null) {
var _ext = (this.ext =
gl.getExtension('WEBGL_draw_buffers'))
if (!_ext)
throw new Error(
'could not instantiate draw buffers extension',
)
this.subKernelOutputTextures = []
this.subKernelOutputVariableNames = []
var _i4 = 0
for (var p in this.subKernelProperties) {
if (
!this.subKernelProperties.hasOwnProperty(
p,
)
)
continue
var _subKernel =
this.subKernelProperties[p]
builder.addSubKernel(_subKernel, {
prototypeOnly: false,
constants: this.constants,
output: this.output,
debug: this.debug,
loopMaxIterations:
this.loopMaxIterations,
})
this.subKernelOutputTextures.push(
this.getSubKernelTexture(p),
)
this.subKernelOutputVariableNames.push(
_subKernel.name + 'Result',
)
_i4++
}
}
},
},
{
key: '_getFragShaderString',
value: function _getFragShaderString(args) {
if (this.compiledFragShaderString !== null) {
return this.compiledFragShaderString
}
return (this.compiledFragShaderString =
this._replaceArtifacts(
fragShaderString,
this._getFragShaderArtifactMap(args),
))
},
},
{
key: '_getVertShaderString',
value: function _getVertShaderString(args) {
if (this.compiledVertShaderString !== null) {
return this.compiledVertShaderString
}
return (this.compiledVertShaderString =
vertShaderString)
},
},
{
key: 'toString',
value: function toString() {
return kernelString(this)
},
},
{
key: 'addFunction',
value: function addFunction(fn) {
this.functionBuilder.addFunction(null, fn)
},
},
])
return WebGLKernel
})(KernelBase)
},
{
'../../core/texture': 23,
'../../core/utils': 25,
'../kernel-base': 8,
'./kernel-string': 13,
'./shader-frag': 16,
'./shader-vert': 17,
},
],
15: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called",
)
}
return call &&
(typeof call === 'object' || typeof call === 'function')
? call
: self
}
function _inherits(subClass, superClass) {
if (
typeof superClass !== 'function' &&
superClass !== null
) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass,
)
}
subClass.prototype = Object.create(
superClass && superClass.prototype,
{
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true,
},
},
)
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass)
}
var RunnerBase = require('../runner-base')
var WebGLKernel = require('./kernel')
var utils = require('../../core/utils')
var WebGLFunctionBuilder = require('./function-builder')
module.exports = (function (_RunnerBase) {
_inherits(WebGLRunner, _RunnerBase)
function WebGLRunner(settings) {
_classCallCheck(this, WebGLRunner)
var _this = _possibleConstructorReturn(
this,
(
WebGLRunner.__proto__ ||
Object.getPrototypeOf(WebGLRunner)
).call(this, new WebGLFunctionBuilder(), settings),
)
_this.Kernel = WebGLKernel
_this.kernel = null
return _this
}
_createClass(WebGLRunner, [
{
key: 'getMode',
value: function getMode() {
return 'gpu'
},
},
])
return WebGLRunner
})(RunnerBase)
},
{
'../../core/utils': 25,
'../runner-base': 10,
'./function-builder': 11,
'./kernel': 14,
},
],
16: [
function (require, module, exports) {
'use strict'
module.exports =
"__HEADER__;\nprecision highp float;\nprecision highp int;\nprecision highp sampler2D;\n\nconst float LOOP_MAX = __LOOP_MAX__;\n#define EPSILON 0.0000001;\n\n__CONSTANTS__;\n\nvarying highp vec2 vTexCoord;\n\nvec4 round(vec4 x) {\n return floor(x + 0.5);\n}\n\nhighp float round(highp float x) {\n return floor(x + 0.5);\n}\n\nvec2 integerMod(vec2 x, float y) {\n vec2 res = floor(mod(x, y));\n return res * step(1.0 - floor(y), -res);\n}\n\nvec3 integerMod(vec3 x, float y) {\n vec3 res = floor(mod(x, y));\n return res * step(1.0 - floor(y), -res);\n}\n\nvec4 integerMod(vec4 x, vec4 y) {\n vec4 res = floor(mod(x, y));\n return res * step(1.0 - floor(y), -res);\n}\n\nhighp float integerMod(highp float x, highp float y) {\n highp float res = floor(mod(x, y));\n return res * (res > floor(y) - 1.0 ? 0.0 : 1.0);\n}\n\nhighp int integerMod(highp int x, highp int y) {\n return int(integerMod(float(x), float(y)));\n}\n\n// Here be dragons!\n// DO NOT OPTIMIZE THIS CODE\n// YOU WILL BREAK SOMETHING ON SOMEBODY'S MACHINE\n// LEAVE IT AS IT IS, LEST YOU WASTE YOUR OWN TIME\nconst vec2 MAGIC_VEC = vec2(1.0, -256.0);\nconst vec4 SCALE_FACTOR = vec4(1.0, 256.0, 65536.0, 0.0);\nconst vec4 SCALE_FACTOR_INV = vec4(1.0, 0.00390625, 0.0000152587890625, 0.0); // 1, 1/256, 1/65536\nhighp float decode32(highp vec4 rgba) {\n __DECODE32_ENDIANNESS__;\n rgba *= 255.0;\n vec2 gte128;\n gte128.x = rgba.b >= 128.0 ? 1.0 : 0.0;\n gte128.y = rgba.a >= 128.0 ? 1.0 : 0.0;\n float exponent = 2.0 * rgba.a - 127.0 + dot(gte128, MAGIC_VEC);\n float res = exp2(round(exponent));\n rgba.b = rgba.b - 128.0 * gte128.x;\n res = dot(rgba, SCALE_FACTOR) * exp2(round(exponent-23.0)) + res;\n res *= gte128.y * -2.0 + 1.0;\n return res;\n}\n\nhighp vec4 encode32(highp float f) {\n highp float F = abs(f);\n highp float sign = f < 0.0 ? 1.0 : 0.0;\n highp float exponent = floor(log2(F));\n highp float mantissa = (exp2(-exponent) * F);\n // exponent += floor(log2(mantissa));\n vec4 rgba = vec4(F * exp2(23.0-exponent)) * SCALE_FACTOR_INV;\n rgba.rg = integerMod(rgba.rg, 256.0);\n rgba.b = integerMod(rgba.b, 128.0);\n rgba.a = exponent*0.5 + 63.5;\n rgba.ba += vec2(integerMod(exponent+127.0, 2.0), sign) * 128.0;\n rgba = floor(rgba);\n rgba *= 0.003921569; // 1/255\n __ENCODE32_ENDIANNESS__;\n return rgba;\n}\n// Dragons end here\n\nhighp float index;\nhighp vec3 threadId;\n\nhighp vec3 indexTo3D(highp float idx, highp vec3 texDim) {\n highp float z = floor(idx / (texDim.x * texDim.y));\n idx -= z * texDim.x * texDim.y;\n highp float y = floor(idx / texDim.x);\n highp float x = integerMod(idx, texDim.x);\n return vec3(x, y, z);\n}\n\nhighp float get(highp sampler2D tex, highp vec2 texSize, highp vec3 texDim, highp float z, highp float y, highp float x) {\n highp vec3 xyz = vec3(x, y, z);\n xyz = floor(xyz + 0.5);\n __GET_WRAPAROUND__;\n highp float index = round(xyz.x + texDim.x * (xyz.y + texDim.y * xyz.z));\n __GET_TEXTURE_CHANNEL__;\n highp float w = round(texSize.x);\n vec2 st = vec2(integerMod(index, w), float(int(index) / int(w))) + 0.5;\n __GET_TEXTURE_INDEX__;\n highp vec4 texel = texture2D(tex, st / texSize);\n __GET_RESULT__;\n}\n\nhighp float get(highp sampler2D tex, highp vec2 texSize, highp vec3 texDim, highp float y, highp float x) {\n return get(tex, texSize, texDim, 0.0, y, x);\n}\n\nhighp float get(highp sampler2D tex, highp vec2 texSize, highp vec3 texDim, highp float x) {\n return get(tex, texSize, texDim, 0.0, 0.0, x);\n}\n\nhighp vec4 actualColor;\nvoid color(float r, float g, float b, float a) {\n actualColor = vec4(r,g,b,a);\n}\n\nvoid color(float r, float g, float b) {\n color(r,g,b,1.0);\n}\n\n__MAIN_PARAMS__;\n__MAIN_CONSTANTS__;\n__KERNEL__;\n\nvoid main(void) {\n index = floor(vTexCoord.s * float(uTexSize.x)) + floor(vTexCoord.t * float(uTexSize.y)) * uTexSize.x;\n __MAIN_RESULT__;\n}"
},
{},
],
17: [
function (require, module, exports) {
'use strict'
module.exports =
'precision highp float;\nprecision highp int;\nprecision highp sampler2D;\n\nattribute highp vec2 aPos;\nattribute highp vec2 aTexCoord;\n\nvarying highp vec2 vTexCoord;\nuniform vec2 ratio;\n\nvoid main(void) {\n gl_Position = vec4((aPos + vec2(1)) * ratio + vec2(-1), 0, 1);\n vTexCoord = aTexCoord;\n}'
},
{},
],
18: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called",
)
}
return call &&
(typeof call === 'object' || typeof call === 'function')
? call
: self
}
function _inherits(subClass, superClass) {
if (
typeof superClass !== 'function' &&
superClass !== null
) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass,
)
}
subClass.prototype = Object.create(
superClass && superClass.prototype,
{
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true,
},
},
)
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass)
}
var WebGLKernel = require('./kernel')
var utils = require('../../core/utils')
module.exports = (function (_WebGLKernel) {
_inherits(WebGLValidatorKernel, _WebGLKernel)
function WebGLValidatorKernel() {
_classCallCheck(this, WebGLValidatorKernel)
return _possibleConstructorReturn(
this,
(
WebGLValidatorKernel.__proto__ ||
Object.getPrototypeOf(WebGLValidatorKernel)
).apply(this, arguments),
)
}
_createClass(WebGLValidatorKernel, [
{
key: 'validateOptions',
value: function validateOptions() {
this.texSize = utils.dimToTexSize(
{
floatTextures: this.floatTextures,
floatOutput: this.floatOutput,
},
this.output,
true,
)
},
},
])
return WebGLValidatorKernel
})(WebGLKernel)
},
{ '../../core/utils': 25, './kernel': 14 },
],
19: [
function (require, module, exports) {
'use strict'
var utils = require('./utils')
module.exports = function alias(name, fn) {
var fnString = fn.toString()
return new Function(
'return function ' +
name +
' (' +
utils.getParamNamesFromString(fnString).join(', ') +
') {' +
utils.getFunctionBodyFromString(fnString) +
'}',
)()
}
},
{ './utils': 25 },
],
20: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
var UtilsCore = require('./utils-core')
module.exports = (function () {
function GPUCore() {
_classCallCheck(this, GPUCore)
}
_createClass(GPUCore, null, [
{
key: 'validateKernelObj',
value: function validateKernelObj(kernelObj) {
if (kernelObj === null) {
throw 'KernelObj being validated is NULL'
}
if (typeof kernelObj === 'string') {
try {
kernelObj = JSON.parse(kernelObj)
} catch (e) {
console.error(e)
throw 'Failed to convert KernelObj from JSON string'
}
if (kernelObj === null) {
throw 'Invalid (NULL) KernelObj JSON string representation'
}
}
if (kernelObj.isKernelObj !== true) {
throw 'Failed missing isKernelObj flag check'
}
return kernelObj
},
},
{
key: 'loadKernelObj',
value: function loadKernelObj(kernelObj, inOpt) {
kernelObj = validateKernelObj(kernelObj)
},
},
])
return GPUCore
})()
},
{ './utils-core': 24 },
],
21: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called",
)
}
return call &&
(typeof call === 'object' || typeof call === 'function')
? call
: self
}
function _inherits(subClass, superClass) {
if (
typeof superClass !== 'function' &&
superClass !== null
) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass,
)
}
subClass.prototype = Object.create(
superClass && superClass.prototype,
{
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true,
},
},
)
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass)
}
var utils = require('./utils')
var WebGLRunner = require('../backend/web-gl/runner')
var CPURunner = require('../backend/cpu/runner')
var WebGLValidatorKernel = require('../backend/web-gl/validator-kernel')
var GPUCore = require('./gpu-core')
var GPU = (function (_GPUCore) {
_inherits(GPU, _GPUCore)
function GPU(settings) {
_classCallCheck(this, GPU)
var _this = _possibleConstructorReturn(
this,
(GPU.__proto__ || Object.getPrototypeOf(GPU)).call(
this,
settings,
),
)
settings = settings || {}
_this._canvas = settings.canvas || null
_this._webGl = settings.webGl || null
var mode = settings.mode
var detectedMode = void 0
if (!utils.isWebGlSupported()) {
if (mode && mode !== 'cpu') {
throw new Error(
'A requested mode of "' +
mode +
'" and is not supported',
)
} else {
console.warn(
'Warning: gpu not supported, falling back to cpu support',
)
detectedMode = 'cpu'
}
} else {
detectedMode = mode || 'gpu'
}
_this.kernels = []
var runnerSettings = {
canvas: _this._canvas,
webGl: _this._webGl,
}
switch (detectedMode) {
case 'cpu':
_this._runner = new CPURunner(runnerSettings)
break
case 'webgl':
case 'gpu':
_this._runner = new WebGLRunner(runnerSettings)
break
case 'webgl-validator':
_this._runner = new WebGLRunner(runnerSettings)
_this._runner.Kernel = WebGLValidatorKernel
break
default:
throw new Error(
'"' + mode + '" mode is not defined',
)
}
return _this
}
_createClass(GPU, [
{
key: 'createKernel',
value: function createKernel(fn, settings) {
if (typeof fn === 'undefined') {
throw 'Missing fn parameter'
}
if (
!utils.isFunction(fn) &&
typeof fn !== 'string'
) {
throw 'fn parameter not a function'
}
var kernel = this._runner.buildKernel(
fn,
settings || {},
)
if (!this._canvas) {
this._canvas = kernel.getCanvas()
}
if (!this._runner.canvas) {
this._runner.canvas = kernel.getCanvas()
}
this.kernels.push(kernel)
return kernel
},
},
{
key: 'createKernelMap',
value: function createKernelMap() {
var fn = void 0
var settings = void 0
if (
typeof arguments[arguments.length - 2] ===
'function'
) {
fn = arguments[arguments.length - 2]
settings = arguments[arguments.length - 1]
} else {
fn = arguments[arguments.length - 1]
}
if (!utils.isWebGlDrawBuffersSupported()) {
this._runner = new CPURunner(settings)
}
var kernel = this.createKernel(fn, settings)
if (Array.isArray(arguments[0])) {
var functions = arguments[0]
for (var i = 0; i < functions.length; i++) {
kernel.addSubKernel(functions[i])
}
} else {
var _functions = arguments[0]
for (var p in _functions) {
if (!_functions.hasOwnProperty(p))
continue
kernel.addSubKernelProperty(
p,
_functions[p],
)
}
}
return kernel
},
},
{
key: 'combineKernels',
value: function combineKernels() {
var lastKernel = arguments[arguments.length - 2]
var combinedKernel =
arguments[arguments.length - 1]
if (this.getMode() === 'cpu')
return combinedKernel
var canvas = arguments[0].getCanvas()
var webGl = arguments[0].getWebGl()
for (var i = 0; i < arguments.length - 1; i++) {
arguments[i]
.setCanvas(canvas)
.setWebGl(webGl)
.setOutputToTexture(true)
}
return function () {
combinedKernel.apply(null, arguments)
var texSize = lastKernel.texSize
var gl = lastKernel.getWebGl()
var threadDim = lastKernel.threadDim
var result = void 0
if (lastKernel.floatOutput) {
result = new Float32Array(
texSize[0] * texSize[1] * 4,
)
gl.readPixels(
0,
0,
texSize[0],
texSize[1],
gl.RGBA,
gl.FLOAT,
result,
)
} else {
var bytes = new Uint8Array(
texSize[0] * texSize[1] * 4,
)
gl.readPixels(
0,
0,
texSize[0],
texSize[1],
gl.RGBA,
gl.UNSIGNED_BYTE,
bytes,
)
result = new Float32Array(bytes.buffer)
}
result = result.subarray(
0,
threadDim[0] *
threadDim[1] *
threadDim[2],
)
if (lastKernel.output.length === 1) {
return result
} else if (lastKernel.output.length === 2) {
return utils.splitArray(
result,
lastKernel.output[0],
)
} else if (lastKernel.output.length === 3) {
var cube = utils.splitArray(
result,
lastKernel.output[0] *
lastKernel.output[1],
)
return cube.map(function (x) {
return utils.splitArray(
x,
lastKernel.output[0],
)
})
}
}
},
},
{
key: 'addFunction',
value: function addFunction(
fn,
paramTypes,
returnType,
) {
this._runner.functionBuilder.addFunction(
null,
fn,
paramTypes,
returnType,
)
return this
},
},
{
key: 'addNativeFunction',
value: function addNativeFunction(
name,
nativeFunction,
) {
this._runner.functionBuilder.addNativeFunction(
name,
nativeFunction,
)
return this
},
},
{
key: 'getMode',
value: function getMode() {
return this._runner.getMode()
},
},
{
key: 'isWebGlSupported',
value: function isWebGlSupported() {
return utils.isWebGlSupported()
},
},
{
key: 'getCanvas',
value: function getCanvas() {
return this._canvas
},
},
{
key: 'getWebGl',
value: function getWebGl() {
return this._webGl
},
},
])
return GPU
})(GPUCore)
Object.assign(GPU, GPUCore)
module.exports = GPU
},
{
'../backend/cpu/runner': 5,
'../backend/web-gl/runner': 15,
'../backend/web-gl/validator-kernel': 18,
'./gpu-core': 20,
'./utils': 25,
},
],
22: [
function (require, module, exports) {
'use strict'
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
module.exports = function Input(value, size) {
_classCallCheck(this, Input)
this.value = value
if (Array.isArray(size)) {
this.size = []
for (var i = 0; i < size.length; i++) {
this.size[i] = size[i]
}
while (this.size.length < 3) {
this.size.push(1)
}
} else {
if (size.z) {
this.size = [size.x, size.y, size.z]
} else if (size.y) {
this.size = [size.x, size.y, 1]
} else {
this.size = [size.x, 1, 1]
}
}
}
},
{},
],
23: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
var gpu = null
module.exports = (function () {
function Texture(texture, size, output, webGl) {
_classCallCheck(this, Texture)
this.texture = texture
this.size = size
this.output = output
this.webGl = webGl
this.kernel = null
}
_createClass(Texture, [
{
key: 'toArray',
value: function toArray(gpu) {
if (!gpu)
throw new Error(
'You need to pass the GPU object for toArray to work.',
)
if (this.kernel) return this.kernel(this)
this.kernel = gpu
.createKernel(function (x) {
return x[
this.thread.z
][this.thread.y][this.thread.x]
})
.setOutput(this.output)
return this.kernel(this)
},
},
{
key: 'delete',
value: function _delete() {
return this.webGl.deleteTexture(this.texture)
},
},
])
return Texture
})()
},
{},
],
24: [
function (require, module, exports) {
'use strict'
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
var UtilsCore = (function () {
function UtilsCore() {
_classCallCheck(this, UtilsCore)
}
_createClass(UtilsCore, null, [
{
key: 'isCanvas',
value: function isCanvas(canvasObj) {
return (
canvasObj !== null &&
canvasObj.nodeName &&
canvasObj.getContext &&
canvasObj.nodeName.toUpperCase() ===
'CANVAS'
)
},
},
{
key: 'isCanvasSupported',
value: function isCanvasSupported() {
return _isCanvasSupported
},
},
{
key: 'initCanvas',
value: function initCanvas() {
if (!_isCanvasSupported) {
return null
}
var canvas = document.createElement('canvas')
canvas.width = 2
canvas.height = 2
return canvas
},
},
{
key: 'isWebGl',
value: function isWebGl(webGlObj) {
return (
webGlObj &&
typeof webGlObj.getExtension === 'function'
)
},
},
{
key: 'isWebGlSupported',
value: function isWebGlSupported() {
return _isWebGlSupported
},
},
{
key: 'isWebGlDrawBuffersSupported',
value: function isWebGlDrawBuffersSupported() {
return _isWebGlDrawBuffersSupported
},
},
{
key: 'initWebGlDefaultOptions',
value: function initWebGlDefaultOptions() {
return {
alpha: false,
depth: false,
antialias: false,
}
},
},
{
key: 'initWebGl',
value: function initWebGl(canvasObj) {
if (
typeof _isCanvasSupported !== 'undefined' ||
canvasObj === null
) {
if (!_isCanvasSupported) {
return null
}
}
if (!UtilsCore.isCanvas(canvasObj)) {
throw new Error(
'Invalid canvas object - ' + canvasObj,
)
}
var webGl =
canvasObj.getContext(
'experimental-webgl',
UtilsCore.initWebGlDefaultOptions(),
) ||
canvasObj.getContext(
'webgl',
UtilsCore.initWebGlDefaultOptions(),
)
if (webGl) {
webGl.OES_texture_float =
webGl.getExtension('OES_texture_float')
webGl.OES_texture_float_linear =
webGl.getExtension(
'OES_texture_float_linear',
)
webGl.OES_element_index_uint =
webGl.getExtension(
'OES_element_index_uint',
)
}
return webGl
},
},
])
return UtilsCore
})()
var _isCanvasSupported =
typeof document !== 'undefined'
? UtilsCore.isCanvas(document.createElement('canvas'))
: false
var _testingWebGl = UtilsCore.initWebGl(UtilsCore.initCanvas())
var _isWebGlSupported = UtilsCore.isWebGl(_testingWebGl)
var _isWebGlDrawBuffersSupported =
_isWebGlSupported &&
Boolean(_testingWebGl.getExtension('WEBGL_draw_buffers'))
if (_isWebGlSupported) {
UtilsCore.OES_texture_float =
_testingWebGl.OES_texture_float
UtilsCore.OES_texture_float_linear =
_testingWebGl.OES_texture_float_linear
UtilsCore.OES_element_index_uint =
_testingWebGl.OES_element_index_uint
} else {
UtilsCore.OES_texture_float = false
UtilsCore.OES_texture_float_linear = false
UtilsCore.OES_element_index_uint = false
}
module.exports = UtilsCore
},
{},
],
25: [
function (require, module, exports) {
'use strict'
var _typeof =
typeof Symbol === 'function' &&
typeof Symbol.iterator === 'symbol'
? function (obj) {
return typeof obj
}
: function (obj) {
return obj &&
typeof Symbol === 'function' &&
obj.constructor === Symbol &&
obj !== Symbol.prototype
? 'symbol'
: typeof obj
}
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable =
descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor)
descriptor.writable = true
Object.defineProperty(
target,
descriptor.key,
descriptor,
)
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps)
defineProperties(Constructor.prototype, protoProps)
if (staticProps)
defineProperties(Constructor, staticProps)
return Constructor
}
})()
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called",
)
}
return call &&
(typeof call === 'object' || typeof call === 'function')
? call
: self
}
function _inherits(subClass, superClass) {
if (
typeof superClass !== 'function' &&
superClass !== null
) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass,
)
}
subClass.prototype = Object.create(
superClass && superClass.prototype,
{
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true,
},
},
)
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass)
}
var UtilsCore = require('./utils-core')
var Input = require('./input')
var Texture = require('./texture')
var FUNCTION_NAME = /function ([^(]*)/
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm
var ARGUMENT_NAMES = /([^\s,]+)/g
var _systemEndianness = (function () {
var b = new ArrayBuffer(4)
var a = new Uint32Array(b)
var c = new Uint8Array(b)
a[0] = 0xdeadbeef
if (c[0] === 0xef) return 'LE'
if (c[0] === 0xde) return 'BE'
throw new Error('unknown endianness')
})()
var _isFloatReadPixelsSupported = null
var _isMixedIdentifiersSupported = (function () {
try {
new Function('let i = 1; const j = 1;')()
return true
} catch (e) {
return false
}
})()
var Utils = (function (_UtilsCore) {
_inherits(Utils, _UtilsCore)
function Utils() {
_classCallCheck(this, Utils)
return _possibleConstructorReturn(
this,
(
Utils.__proto__ || Object.getPrototypeOf(Utils)
).apply(this, arguments),
)
}
_createClass(Utils, null, [
{
key: 'systemEndianness',
value: function systemEndianness() {
return _systemEndianness
},
},
{
key: 'isFunction',
value: function isFunction(funcObj) {
return typeof funcObj === 'function'
},
},
{
key: 'isFunctionString',
value: function isFunctionString(funcStr) {
if (funcStr !== null) {
return (
funcStr
.toString()
.slice(0, 'function'.length)
.toLowerCase() === 'function'
)
}
return false
},
},
{
key: 'getFunctionNameFromString',
value: function getFunctionNameFromString(funcStr) {
return FUNCTION_NAME.exec(funcStr)[1]
},
},
{
key: 'getFunctionBodyFromString',
value: function getFunctionBodyFromString(funcStr) {
return funcStr.substring(
funcStr.indexOf('{') + 1,
funcStr.lastIndexOf('}'),
)
},
},
{
key: 'getParamNamesFromString',
value: function getParamNamesFromString(func) {
var fnStr = func
.toString()
.replace(STRIP_COMMENTS, '')
var result = fnStr
.slice(
fnStr.indexOf('(') + 1,
fnStr.indexOf(')'),
)
.match(ARGUMENT_NAMES)
if (result === null) result = []
return result
},
},
{
key: 'clone',
value: function clone(obj) {
if (
obj === null ||
(typeof obj === 'undefined'
? 'undefined'
: _typeof(obj)) !== 'object' ||
obj.hasOwnProperty('isActiveClone')
)
return obj
var temp = obj.constructor()
for (var key in obj) {
if (
Object.prototype.hasOwnProperty.call(
obj,
key,
)
) {
obj.isActiveClone = null
temp[key] = Utils.clone(obj[key])
delete obj.isActiveClone
}
}
return temp
},
},
{
key: 'newPromise',
value: function newPromise(executor) {
var simple = Promise || small_promise
if (simple === null) {
throw TypeError(
'Browser is missing Promise implementation. Consider adding small_promise.js polyfill',
)
}
return new simple(executor)
},
},
{
key: 'functionBinder',
value: function functionBinder(inFunc, thisObj) {
if (inFunc.bind) {
return inFunc.bind(thisObj)
}
return function () {
var args =
arguments.length === 1
? [arguments[0]]
: Array.apply(null, arguments)
return inFunc.apply(thisObj, args)
}
},
},
{
key: 'isArray',
value: function isArray(array) {
if (isNaN(array.length)) {
return false
}
return true
},
},
{
key: 'getArgumentType',
value: function getArgumentType(arg) {
if (Utils.isArray(arg)) {
return 'Array'
} else if (typeof arg === 'number') {
return 'Number'
} else if (arg instanceof Texture) {
return 'Texture'
} else if (arg instanceof Input) {
return 'Input'
} else {
return 'Unknown'
}
},
},
{
key: 'isFloatReadPixelsSupported',
value: function isFloatReadPixelsSupported() {
if (_isFloatReadPixelsSupported !== null) {
return _isFloatReadPixelsSupported
}
var GPU = require('../index')
var x = new GPU({
mode: 'webgl-validator',
}).createKernel(
function () {
return 1
},
{
output: [2],
floatTextures: true,
floatOutput: true,
floatOutputForce: true,
},
)()
_isFloatReadPixelsSupported = x[0] === 1
return _isFloatReadPixelsSupported
},
},
{
key: 'isMixedIdentifiersSupported',
value: function isMixedIdentifiersSupported() {
return _isMixedIdentifiersSupported
},
},
{
key: 'dimToTexSize',
value: function dimToTexSize(
opt,
dimensions,
output,
) {
var numTexels = dimensions[0]
for (var i = 1; i < dimensions.length; i++) {
numTexels *= dimensions[i]
}
if (
opt.floatTextures &&
(!output || opt.floatOutput)
) {
numTexels = Math.ceil(numTexels / 4)
}
var w = Math.ceil(Math.sqrt(numTexels))
return [w, w]
},
},
{
key: 'getDimensions',
value: function getDimensions(x, pad) {
var ret = void 0
if (Utils.isArray(x)) {
var dim = []
var temp = x
while (Utils.isArray(temp)) {
dim.push(temp.length)
temp = temp[0]
}
ret = dim.reverse()
} else if (x instanceof Texture) {
ret = x.output
} else if (x instanceof Input) {
ret = x.size
} else {
throw 'Unknown dimensions of ' + x
}
if (pad) {
ret = Utils.clone(ret)
while (ret.length < 3) {
ret.push(1)
}
}
return ret
},
},
{
key: 'pad',
value: function pad(arr, padding) {
function zeros(n) {
return Array.apply(null, new Array(n)).map(
Number.prototype.valueOf,
0,
)
}
var len = arr.length + padding * 2
var ret = arr.map(function (x) {
return [].concat(
zeros(padding),
x,
zeros(padding),
)
})
for (var i = 0; i < padding; i++) {
ret = [].concat([zeros(len)], ret, [
zeros(len),
])
}
return ret
},
},
{
key: 'flatten2dArrayTo',
value: function flatten2dArrayTo(array, target) {
var offset = 0
for (var y = 0; y < array.length; y++) {
target.set(array[y], offset)
offset += array[y].length
}
},
},
{
key: 'flatten3dArrayTo',
value: function flatten3dArrayTo(array, target) {
var offset = 0
for (var z = 0; z < array.length; z++) {
for (var y = 0; y < array[z].length; y++) {
target.set(array[z][y], offset)
offset += array[z][y].length
}
}
},
},
{
key: 'flattenTo',
value: function flattenTo(array, target) {
if (Utils.isArray(array[0])) {
if (Utils.isArray(array[0][0])) {
Utils.flatten3dArrayTo(array, target)
} else {
Utils.flatten2dArrayTo(array, target)
}
} else {
target.set(array)
}
},
},
{
key: 'splitArray',
value: function splitArray(array, part) {
var result = []
for (var i = 0; i < array.length; i += part) {
result.push(
Array.prototype.slice.call(
array,
i,
i + part,
),
)
}
return result
},
},
{
key: 'getAstString',
value: function getAstString(source, ast) {
var lines = Array.isArray(source)
? source
: source.split(/\r?\n/g)
var start = ast.loc.start
var end = ast.loc.end
var result = []
result.push(
lines[start.line - 1].slice(start.column),
)
for (
var i = start.line;
i < end.line - 1;
i++
) {
result.push(lines[i])
}
result.push(
lines[end.line - 1].slice(0, end.column),
)
return result.join('\n')
},
},
{
key: 'allPropertiesOf',
value: function allPropertiesOf(obj) {
var props = []
do {
props.push.apply(
props,
Object.getOwnPropertyNames(obj),
)
} while ((obj = Object.getPrototypeOf(obj)))
return props
},
},
])
return Utils
})(UtilsCore)
Object.assign(Utils, UtilsCore)
module.exports = Utils
},
{
'../index': 26,
'./input': 22,
'./texture': 23,
'./utils-core': 24,
},
],
26: [
function (require, module, exports) {
'use strict'
var GPU = require('./core/gpu')
var alias = require('./core/alias')
var utils = require('./core/utils')
var Input = require('./core/input')
var Texture = require('./core/texture')
var CPUFunctionBuilder = require('./backend/cpu/function-builder')
var CPUFunctionNode = require('./backend/cpu/function-node')
var CPUKernel = require('./backend/cpu/kernel')
var CPURunner = require('./backend/cpu/runner')
var WebGLFunctionBuilder = require('./backend/web-gl/function-builder')
var WebGLFunctionNode = require('./backend/web-gl/function-node')
var WebGLKernel = require('./backend/web-gl/kernel')
var WebGLRunner = require('./backend/web-gl/runner')
GPU.alias = alias
GPU.utils = utils
GPU.Texture = Texture
GPU.Input = Input
GPU.input = function (value, size) {
return new Input(value, size)
}
GPU.CPUFunctionBuilder = CPUFunctionBuilder
GPU.CPUFunctionNode = CPUFunctionNode
GPU.CPUKernel = CPUKernel
GPU.CPURunner = CPURunner
GPU.WebGLFunctionBuilder = WebGLFunctionBuilder
GPU.WebGLFunctionNode = WebGLFunctionNode
GPU.WebGLKernel = WebGLKernel
GPU.WebGLRunner = WebGLRunner
if (typeof module !== 'undefined') {
module.exports = GPU
}
if (typeof window !== 'undefined') {
window.GPU = GPU
}
},
{
'./backend/cpu/function-builder': 1,
'./backend/cpu/function-node': 2,
'./backend/cpu/kernel': 4,
'./backend/cpu/runner': 5,
'./backend/web-gl/function-builder': 11,
'./backend/web-gl/function-node': 12,
'./backend/web-gl/kernel': 14,
'./backend/web-gl/runner': 15,
'./core/alias': 19,
'./core/gpu': 21,
'./core/input': 22,
'./core/texture': 23,
'./core/utils': 25,
},
],
27: [
function (require, module, exports) {
;(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined'
? factory(exports)
: typeof define === 'function' && define.amd
? define(['exports'], factory)
: factory((global.acorn = global.acorn || {}))
})(this, function (exports) {
'use strict'
var reservedWords = {
3: 'abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile',
5: 'class enum extends super const export import',
6: 'enum',
strict: 'implements interface let package private protected public static yield',
strictBind: 'eval arguments',
}
var ecma5AndLessKeywords =
'break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in this'
var keywords = {
5: ecma5AndLessKeywords,
6:
ecma5AndLessKeywords +
' const class extends export import super',
}
var nonASCIIidentifierStartChars =
'\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0-\u08b4\u08b6-\u08bd\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fd5\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7ae\ua7b0-\ua7b7\ua7f7-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab65\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc'
var nonASCIIidentifierChars =
'\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08d4-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c03\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d01-\u0d03\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0eb9\u0ebb\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf2-\u1cf4\u1cf8\u1cf9\u1dc0-\u1df5\u1dfb-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua900-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f'
var nonASCIIidentifierStart = new RegExp(
'[' + nonASCIIidentifierStartChars + ']',
)
var nonASCIIidentifier = new RegExp(
'[' +
nonASCIIidentifierStartChars +
nonASCIIidentifierChars +
']',
)
nonASCIIidentifierStartChars = nonASCIIidentifierChars =
null
var astralIdentifierStartCodes = [
0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70,
52, 268, 28, 4, 48, 48, 31, 17, 26, 6, 37, 11, 29, 3,
35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51,
157, 310, 10, 21, 11, 7, 153, 5, 3, 0, 2, 43, 2, 1, 4,
0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25,
71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 26, 45, 28, 4, 28,
36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72,
56, 50, 14, 50, 785, 52, 76, 44, 33, 24, 27, 35, 42, 34,
4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 85, 6,
2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2,
6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 54,
47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42,
86, 25, 391, 63, 32, 0, 449, 56, 264, 8, 2, 36, 18, 0,
50, 29, 881, 921, 103, 110, 18, 195, 2749, 1070, 4050,
582, 8634, 568, 8, 30, 114, 29, 19, 47, 17, 3, 32, 20,
6, 18, 881, 68, 12, 0, 67, 12, 65, 0, 32, 6124, 20, 754,
9486, 1, 3071, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70,
2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3,
3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3,
24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2,
30, 2, 24, 2, 7, 4149, 196, 60, 67, 1213, 3, 2, 26, 2,
1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0,
2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2,
1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6,
2, 2, 4, 2, 16, 4421, 42710, 42, 4148, 12, 221, 3, 5761,
10591, 541,
]
var astralIdentifierCodes = [
509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41,
2, 5, 0, 166, 1, 1306, 2, 54, 14, 32, 9, 16, 3, 46, 10,
54, 9, 7, 2, 37, 13, 2, 9, 52, 0, 13, 2, 49, 13, 10, 2,
4, 9, 83, 11, 7, 0, 161, 11, 6, 9, 7, 3, 57, 0, 2, 6, 3,
1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 87,
19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12,
9, 9, 84, 14, 5, 9, 423, 9, 838, 7, 2, 7, 17, 9, 57, 21,
2, 13, 19882, 9, 135, 4, 60, 6, 26, 9, 1016, 45, 17, 3,
19723, 1, 5319, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2,
1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14,
1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 2214, 6, 110, 6, 6, 9,
792487, 239,
]
function isInAstralSet(code, set) {
var pos = 0x10000
for (var i = 0; i < set.length; i += 2) {
pos += set[i]
if (pos > code) {
return false
}
pos += set[i + 1]
if (pos >= code) {
return true
}
}
}
function isIdentifierStart(code, astral) {
if (code < 65) {
return code === 36
}
if (code < 91) {
return true
}
if (code < 97) {
return code === 95
}
if (code < 123) {
return true
}
if (code <= 0xffff) {
return (
code >= 0xaa &&
nonASCIIidentifierStart.test(
String.fromCharCode(code),
)
)
}
if (astral === false) {
return false
}
return isInAstralSet(code, astralIdentifierStartCodes)
}
function isIdentifierChar(code, astral) {
if (code < 48) {
return code === 36
}
if (code < 58) {
return true
}
if (code < 65) {
return false
}
if (code < 91) {
return true
}
if (code < 97) {
return code === 95
}
if (code < 123) {
return true
}
if (code <= 0xffff) {
return (
code >= 0xaa &&
nonASCIIidentifier.test(
String.fromCharCode(code),
)
)
}
if (astral === false) {
return false
}
return (
isInAstralSet(code, astralIdentifierStartCodes) ||
isInAstralSet(code, astralIdentifierCodes)
)
}
var TokenType = function TokenType(label, conf) {
if (conf === void 0) conf = {}
this.label = label
this.keyword = conf.keyword
this.beforeExpr = !!conf.beforeExpr
this.startsExpr = !!conf.startsExpr
this.isLoop = !!conf.isLoop
this.isAssign = !!conf.isAssign
this.prefix = !!conf.prefix
this.postfix = !!conf.postfix
this.binop = conf.binop || null
this.updateContext = null
}
function binop(name, prec) {
return new TokenType(name, {
beforeExpr: true,
binop: prec,
})
}
var beforeExpr = { beforeExpr: true }
var startsExpr = { startsExpr: true }
var keywords$1 = {}
function kw(name, options) {
if (options === void 0) options = {}
options.keyword = name
return (keywords$1[name] = new TokenType(name, options))
}
var types = {
num: new TokenType('num', startsExpr),
regexp: new TokenType('regexp', startsExpr),
string: new TokenType('string', startsExpr),
name: new TokenType('name', startsExpr),
eof: new TokenType('eof'),
bracketL: new TokenType('[', {
beforeExpr: true,
startsExpr: true,
}),
bracketR: new TokenType(']'),
braceL: new TokenType('{', {
beforeExpr: true,
startsExpr: true,
}),
braceR: new TokenType('}'),
parenL: new TokenType('(', {
beforeExpr: true,
startsExpr: true,
}),
parenR: new TokenType(')'),
comma: new TokenType(',', beforeExpr),
semi: new TokenType(';', beforeExpr),
colon: new TokenType(':', beforeExpr),
dot: new TokenType('.'),
question: new TokenType('?', beforeExpr),
arrow: new TokenType('=>', beforeExpr),
template: new TokenType('template'),
invalidTemplate: new TokenType('invalidTemplate'),
ellipsis: new TokenType('...', beforeExpr),
backQuote: new TokenType('`', startsExpr),
dollarBraceL: new TokenType('${', {
beforeExpr: true,
startsExpr: true,
}),
eq: new TokenType('=', {
beforeExpr: true,
isAssign: true,
}),
assign: new TokenType('_=', {
beforeExpr: true,
isAssign: true,
}),
incDec: new TokenType('++/--', {
prefix: true,
postfix: true,
startsExpr: true,
}),
prefix: new TokenType('!/~', {
beforeExpr: true,
prefix: true,
startsExpr: true,
}),
logicalOR: binop('||', 1),
logicalAND: binop('&&', 2),
bitwiseOR: binop('|', 3),
bitwiseXOR: binop('^', 4),
bitwiseAND: binop('&', 5),
equality: binop('==/!=/===/!==', 6),
relational: binop('</>/<=/>=', 7),
bitShift: binop('<</>>/>>>', 8),
plusMin: new TokenType('+/-', {
beforeExpr: true,
binop: 9,
prefix: true,
startsExpr: true,
}),
modulo: binop('%', 10),
star: binop('*', 10),
slash: binop('/', 10),
starstar: new TokenType('**', { beforeExpr: true }),
_break: kw('break'),
_case: kw('case', beforeExpr),
_catch: kw('catch'),
_continue: kw('continue'),
_debugger: kw('debugger'),
_default: kw('default', beforeExpr),
_do: kw('do', { isLoop: true, beforeExpr: true }),
_else: kw('else', beforeExpr),
_finally: kw('finally'),
_for: kw('for', { isLoop: true }),
_function: kw('function', startsExpr),
_if: kw('if'),
_return: kw('return', beforeExpr),
_switch: kw('switch'),
_throw: kw('throw', beforeExpr),
_try: kw('try'),
_var: kw('var'),
_const: kw('const'),
_while: kw('while', { isLoop: true }),
_with: kw('with'),
_new: kw('new', { beforeExpr: true, startsExpr: true }),
_this: kw('this', startsExpr),
_super: kw('super', startsExpr),
_class: kw('class', startsExpr),
_extends: kw('extends', beforeExpr),
_export: kw('export'),
_import: kw('import'),
_null: kw('null', startsExpr),
_true: kw('true', startsExpr),
_false: kw('false', startsExpr),
_in: kw('in', { beforeExpr: true, binop: 7 }),
_instanceof: kw('instanceof', {
beforeExpr: true,
binop: 7,
}),
_typeof: kw('typeof', {
beforeExpr: true,
prefix: true,
startsExpr: true,
}),
_void: kw('void', {
beforeExpr: true,
prefix: true,
startsExpr: true,
}),
_delete: kw('delete', {
beforeExpr: true,
prefix: true,
startsExpr: true,
}),
}
var lineBreak = /\r\n?|\n|\u2028|\u2029/
var lineBreakG = new RegExp(lineBreak.source, 'g')
function isNewLine(code) {
return (
code === 10 ||
code === 13 ||
code === 0x2028 ||
code === 0x2029
)
}
var nonASCIIwhitespace =
/[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/
var skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g
var ref = Object.prototype
var hasOwnProperty = ref.hasOwnProperty
var toString = ref.toString
function has(obj, propName) {
return hasOwnProperty.call(obj, propName)
}
var isArray =
Array.isArray ||
function (obj) {
return toString.call(obj) === '[object Array]'
}
var Position = function Position(line, col) {
this.line = line
this.column = col
}
Position.prototype.offset = function offset(n) {
return new Position(this.line, this.column + n)
}
var SourceLocation = function SourceLocation(
p,
start,
end,
) {
this.start = start
this.end = end
if (p.sourceFile !== null) {
this.source = p.sourceFile
}
}
function getLineInfo(input, offset) {
for (var line = 1, cur = 0; ; ) {
lineBreakG.lastIndex = cur
var match = lineBreakG.exec(input)
if (match && match.index < offset) {
++line
cur = match.index + match[0].length
} else {
return new Position(line, offset - cur)
}
}
}
var defaultOptions = {
ecmaVersion: 7,
sourceType: 'script',
onInsertedSemicolon: null,
onTrailingComma: null,
allowReserved: null,
allowReturnOutsideFunction: false,
allowImportExportEverywhere: false,
allowHashBang: false,
locations: false,
onToken: null,
onComment: null,
ranges: false,
program: null,
sourceFile: null,
directSourceFile: null,
preserveParens: false,
plugins: {},
}
function getOptions(opts) {
var options = {}
for (var opt in defaultOptions) {
options[opt] =
opts && has(opts, opt)
? opts[opt]
: defaultOptions[opt]
}
if (options.ecmaVersion >= 2015) {
options.ecmaVersion -= 2009
}
if (options.allowReserved == null) {
options.allowReserved = options.ecmaVersion < 5
}
if (isArray(options.onToken)) {
var tokens = options.onToken
options.onToken = function (token) {
return tokens.push(token)
}
}
if (isArray(options.onComment)) {
options.onComment = pushComment(
options,
options.onComment,
)
}
return options
}
function pushComment(options, array) {
return function (
block,
text,
start,
end,
startLoc,
endLoc,
) {
var comment = {
type: block ? 'Block' : 'Line',
value: text,
start: start,
end: end,
}
if (options.locations) {
comment.loc = new SourceLocation(
this,
startLoc,
endLoc,
)
}
if (options.ranges) {
comment.range = [start, end]
}
array.push(comment)
}
}
var plugins = {}
function keywordRegexp(words) {
return new RegExp(
'^(?:' + words.replace(/ /g, '|') + ')$',
)
}
var Parser = function Parser(options, input, startPos) {
this.options = options = getOptions(options)
this.sourceFile = options.sourceFile
this.keywords = keywordRegexp(
keywords[options.ecmaVersion >= 6 ? 6 : 5],
)
var reserved = ''
if (!options.allowReserved) {
for (var v = options.ecmaVersion; ; v--) {
if ((reserved = reservedWords[v])) {
break
}
}
if (options.sourceType == 'module') {
reserved += ' await'
}
}
this.reservedWords = keywordRegexp(reserved)
var reservedStrict =
(reserved ? reserved + ' ' : '') +
reservedWords.strict
this.reservedWordsStrict = keywordRegexp(reservedStrict)
this.reservedWordsStrictBind = keywordRegexp(
reservedStrict + ' ' + reservedWords.strictBind,
)
this.input = String(input)
this.containsEsc = false
this.loadPlugins(options.plugins)
if (startPos) {
this.pos = startPos
this.lineStart =
this.input.lastIndexOf('\n', startPos - 1) + 1
this.curLine = this.input
.slice(0, this.lineStart)
.split(lineBreak).length
} else {
this.pos = this.lineStart = 0
this.curLine = 1
}
this.type = types.eof
this.value = null
this.start = this.end = this.pos
this.startLoc = this.endLoc = this.curPosition()
this.lastTokEndLoc = this.lastTokStartLoc = null
this.lastTokStart = this.lastTokEnd = this.pos
this.context = this.initialContext()
this.exprAllowed = true
this.inModule = options.sourceType === 'module'
this.strict =
this.inModule || this.strictDirective(this.pos)
this.potentialArrowAt = -1
this.inFunction =
this.inGenerator =
this.inAsync =
false
this.yieldPos = this.awaitPos = 0
this.labels = []
if (
this.pos === 0 &&
options.allowHashBang &&
this.input.slice(0, 2) === '#!'
) {
this.skipLineComment(2)
}
this.scopeStack = []
this.enterFunctionScope()
}
Parser.prototype.isKeyword = function isKeyword(word) {
return this.keywords.test(word)
}
Parser.prototype.isReservedWord = function isReservedWord(
word,
) {
return this.reservedWords.test(word)
}
Parser.prototype.extend = function extend(name, f) {
this[name] = f(this[name])
}
Parser.prototype.loadPlugins = function loadPlugins(
pluginConfigs,
) {
var this$1 = this
for (var name in pluginConfigs) {
var plugin = plugins[name]
if (!plugin) {
throw new Error(
"Plugin '" + name + "' not found",
)
}
plugin(this$1, pluginConfigs[name])
}
}
Parser.prototype.parse = function parse() {
var node = this.options.program || this.startNode()
this.nextToken()
return this.parseTopLevel(node)
}
var pp = Parser.prototype
var literal = /^(?:'((?:\\.|[^'])*?)'|"((?:\\.|[^"])*?)"|;)/
pp.strictDirective = function (start) {
var this$1 = this
for (;;) {
skipWhiteSpace.lastIndex = start
start += skipWhiteSpace.exec(this$1.input)[0].length
var match = literal.exec(this$1.input.slice(start))
if (!match) {
return false
}
if ((match[1] || match[2]) == 'use strict') {
return true
}
start += match[0].length
}
}
pp.eat = function (type) {
if (this.type === type) {
this.next()
return true
} else {
return false
}
}
pp.isContextual = function (name) {
return this.type === types.name && this.value === name
}
pp.eatContextual = function (name) {
return this.value === name && this.eat(types.name)
}
pp.expectContextual = function (name) {
if (!this.eatContextual(name)) {
this.unexpected()
}
}
pp.canInsertSemicolon = function () {
return (
this.type === types.eof ||
this.type === types.braceR ||
lineBreak.test(
this.input.slice(this.lastTokEnd, this.start),
)
)
}
pp.insertSemicolon = function () {
if (this.canInsertSemicolon()) {
if (this.options.onInsertedSemicolon) {
this.options.onInsertedSemicolon(
this.lastTokEnd,
this.lastTokEndLoc,
)
}
return true
}
}
pp.semicolon = function () {
if (!this.eat(types.semi) && !this.insertSemicolon()) {
this.unexpected()
}
}
pp.afterTrailingComma = function (tokType, notNext) {
if (this.type == tokType) {
if (this.options.onTrailingComma) {
this.options.onTrailingComma(
this.lastTokStart,
this.lastTokStartLoc,
)
}
if (!notNext) {
this.next()
}
return true
}
}
pp.expect = function (type) {
this.eat(type) || this.unexpected()
}
pp.unexpected = function (pos) {
this.raise(
pos != null ? pos : this.start,
'Unexpected token',
)
}
function DestructuringErrors() {
this.shorthandAssign =
this.trailingComma =
this.parenthesizedAssign =
this.parenthesizedBind =
-1
}
pp.checkPatternErrors = function (
refDestructuringErrors,
isAssign,
) {
if (!refDestructuringErrors) {
return
}
if (refDestructuringErrors.trailingComma > -1) {
this.raiseRecoverable(
refDestructuringErrors.trailingComma,
'Comma is not permitted after the rest element',
)
}
var parens = isAssign
? refDestructuringErrors.parenthesizedAssign
: refDestructuringErrors.parenthesizedBind
if (parens > -1) {
this.raiseRecoverable(
parens,
'Parenthesized pattern',
)
}
}
pp.checkExpressionErrors = function (
refDestructuringErrors,
andThrow,
) {
var pos = refDestructuringErrors
? refDestructuringErrors.shorthandAssign
: -1
if (!andThrow) {
return pos >= 0
}
if (pos > -1) {
this.raise(
pos,
'Shorthand property assignments are valid only in destructuring patterns',
)
}
}
pp.checkYieldAwaitInDefaultParams = function () {
if (
this.yieldPos &&
(!this.awaitPos || this.yieldPos < this.awaitPos)
) {
this.raise(
this.yieldPos,
'Yield expression cannot be a default value',
)
}
if (this.awaitPos) {
this.raise(
this.awaitPos,
'Await expression cannot be a default value',
)
}
}
pp.isSimpleAssignTarget = function (expr) {
if (expr.type === 'ParenthesizedExpression') {
return this.isSimpleAssignTarget(expr.expression)
}
return (
expr.type === 'Identifier' ||
expr.type === 'MemberExpression'
)
}
var pp$1 = Parser.prototype
pp$1.parseTopLevel = function (node) {
var this$1 = this
var exports = {}
if (!node.body) {
node.body = []
}
while (this.type !== types.eof) {
var stmt = this$1.parseStatement(
true,
true,
exports,
)
node.body.push(stmt)
}
this.adaptDirectivePrologue(node.body)
this.next()
if (this.options.ecmaVersion >= 6) {
node.sourceType = this.options.sourceType
}
return this.finishNode(node, 'Program')
}
var loopLabel = { kind: 'loop' }
var switchLabel = { kind: 'switch' }
pp$1.isLet = function () {
if (
this.type !== types.name ||
this.options.ecmaVersion < 6 ||
this.value != 'let'
) {
return false
}
skipWhiteSpace.lastIndex = this.pos
var skip = skipWhiteSpace.exec(this.input)
var next = this.pos + skip[0].length,
nextCh = this.input.charCodeAt(next)
if (nextCh === 91 || nextCh == 123) {
return true
}
if (isIdentifierStart(nextCh, true)) {
var pos = next + 1
while (
isIdentifierChar(
this.input.charCodeAt(pos),
true,
)
) {
++pos
}
var ident = this.input.slice(next, pos)
if (!this.isKeyword(ident)) {
return true
}
}
return false
}
pp$1.isAsyncFunction = function () {
if (
this.type !== types.name ||
this.options.ecmaVersion < 8 ||
this.value != 'async'
) {
return false
}
skipWhiteSpace.lastIndex = this.pos
var skip = skipWhiteSpace.exec(this.input)
var next = this.pos + skip[0].length
return (
!lineBreak.test(this.input.slice(this.pos, next)) &&
this.input.slice(next, next + 8) === 'function' &&
(next + 8 == this.input.length ||
!isIdentifierChar(this.input.charAt(next + 8)))
)
}
pp$1.parseStatement = function (
declaration,
topLevel,
exports,
) {
var starttype = this.type,
node = this.startNode(),
kind
if (this.isLet()) {
starttype = types._var
kind = 'let'
}
switch (starttype) {
case types._break:
case types._continue:
return this.parseBreakContinueStatement(
node,
starttype.keyword,
)
case types._debugger:
return this.parseDebuggerStatement(node)
case types._do:
return this.parseDoStatement(node)
case types._for:
return this.parseForStatement(node)
case types._function:
if (
!declaration &&
this.options.ecmaVersion >= 6
) {
this.unexpected()
}
return this.parseFunctionStatement(node, false)
case types._class:
if (!declaration) {
this.unexpected()
}
return this.parseClass(node, true)
case types._if:
return this.parseIfStatement(node)
case types._return:
return this.parseReturnStatement(node)
case types._switch:
return this.parseSwitchStatement(node)
case types._throw:
return this.parseThrowStatement(node)
case types._try:
return this.parseTryStatement(node)
case types._const:
case types._var:
kind = kind || this.value
if (!declaration && kind != 'var') {
this.unexpected()
}
return this.parseVarStatement(node, kind)
case types._while:
return this.parseWhileStatement(node)
case types._with:
return this.parseWithStatement(node)
case types.braceL:
return this.parseBlock()
case types.semi:
return this.parseEmptyStatement(node)
case types._export:
case types._import:
if (!this.options.allowImportExportEverywhere) {
if (!topLevel) {
this.raise(
this.start,
"'import' and 'export' may only appear at the top level",
)
}
if (!this.inModule) {
this.raise(
this.start,
"'import' and 'export' may appear only with 'sourceType: module'",
)
}
}
return starttype === types._import
? this.parseImport(node)
: this.parseExport(node, exports)
default:
if (this.isAsyncFunction() && declaration) {
this.next()
return this.parseFunctionStatement(
node,
true,
)
}
var maybeName = this.value,
expr = this.parseExpression()
if (
starttype === types.name &&
expr.type === 'Identifier' &&
this.eat(types.colon)
) {
return this.parseLabeledStatement(
node,
maybeName,
expr,
)
} else {
return this.parseExpressionStatement(
node,
expr,
)
}
}
}
pp$1.parseBreakContinueStatement = function (
node,
keyword,
) {
var this$1 = this
var isBreak = keyword == 'break'
this.next()
if (this.eat(types.semi) || this.insertSemicolon()) {
node.label = null
} else if (this.type !== types.name) {
this.unexpected()
} else {
node.label = this.parseIdent()
this.semicolon()
}
var i = 0
for (; i < this.labels.length; ++i) {
var lab = this$1.labels[i]
if (
node.label == null ||
lab.name === node.label.name
) {
if (
lab.kind != null &&
(isBreak || lab.kind === 'loop')
) {
break
}
if (node.label && isBreak) {
break
}
}
}
if (i === this.labels.length) {
this.raise(node.start, 'Unsyntactic ' + keyword)
}
return this.finishNode(
node,
isBreak ? 'BreakStatement' : 'ContinueStatement',
)
}
pp$1.parseDebuggerStatement = function (node) {
this.next()
this.semicolon()
return this.finishNode(node, 'DebuggerStatement')
}
pp$1.parseDoStatement = function (node) {
this.next()
this.labels.push(loopLabel)
node.body = this.parseStatement(false)
this.labels.pop()
this.expect(types._while)
node.test = this.parseParenExpression()
if (this.options.ecmaVersion >= 6) {
this.eat(types.semi)
} else {
this.semicolon()
}
return this.finishNode(node, 'DoWhileStatement')
}
pp$1.parseForStatement = function (node) {
this.next()
this.labels.push(loopLabel)
this.enterLexicalScope()
this.expect(types.parenL)
if (this.type === types.semi) {
return this.parseFor(node, null)
}
var isLet = this.isLet()
if (
this.type === types._var ||
this.type === types._const ||
isLet
) {
var init$1 = this.startNode(),
kind = isLet ? 'let' : this.value
this.next()
this.parseVar(init$1, true, kind)
this.finishNode(init$1, 'VariableDeclaration')
if (
(this.type === types._in ||
(this.options.ecmaVersion >= 6 &&
this.isContextual('of'))) &&
init$1.declarations.length === 1 &&
!(kind !== 'var' && init$1.declarations[0].init)
) {
return this.parseForIn(node, init$1)
}
return this.parseFor(node, init$1)
}
var refDestructuringErrors = new DestructuringErrors()
var init = this.parseExpression(
true,
refDestructuringErrors,
)
if (
this.type === types._in ||
(this.options.ecmaVersion >= 6 &&
this.isContextual('of'))
) {
this.toAssignable(init)
this.checkLVal(init)
this.checkPatternErrors(
refDestructuringErrors,
true,
)
return this.parseForIn(node, init)
} else {
this.checkExpressionErrors(
refDestructuringErrors,
true,
)
}
return this.parseFor(node, init)
}
pp$1.parseFunctionStatement = function (node, isAsync) {
this.next()
return this.parseFunction(node, true, false, isAsync)
}
pp$1.isFunction = function () {
return (
this.type === types._function ||
this.isAsyncFunction()
)
}
pp$1.parseIfStatement = function (node) {
this.next()
node.test = this.parseParenExpression()
node.consequent = this.parseStatement(
!this.strict && this.isFunction(),
)
node.alternate = this.eat(types._else)
? this.parseStatement(
!this.strict && this.isFunction(),
)
: null
return this.finishNode(node, 'IfStatement')
}
pp$1.parseReturnStatement = function (node) {
if (
!this.inFunction &&
!this.options.allowReturnOutsideFunction
) {
this.raise(
this.start,
"'return' outside of function",
)
}
this.next()
if (this.eat(types.semi) || this.insertSemicolon()) {
node.argument = null
} else {
node.argument = this.parseExpression()
this.semicolon()
}
return this.finishNode(node, 'ReturnStatement')
}
pp$1.parseSwitchStatement = function (node) {
var this$1 = this
this.next()
node.discriminant = this.parseParenExpression()
node.cases = []
this.expect(types.braceL)
this.labels.push(switchLabel)
this.enterLexicalScope()
var cur
for (
var sawDefault = false;
this.type != types.braceR;
) {
if (
this$1.type === types._case ||
this$1.type === types._default
) {
var isCase = this$1.type === types._case
if (cur) {
this$1.finishNode(cur, 'SwitchCase')
}
node.cases.push((cur = this$1.startNode()))
cur.consequent = []
this$1.next()
if (isCase) {
cur.test = this$1.parseExpression()
} else {
if (sawDefault) {
this$1.raiseRecoverable(
this$1.lastTokStart,
'Multiple default clauses',
)
}
sawDefault = true
cur.test = null
}
this$1.expect(types.colon)
} else {
if (!cur) {
this$1.unexpected()
}
cur.consequent.push(this$1.parseStatement(true))
}
}
this.exitLexicalScope()
if (cur) {
this.finishNode(cur, 'SwitchCase')
}
this.next()
this.labels.pop()
return this.finishNode(node, 'SwitchStatement')
}
pp$1.parseThrowStatement = function (node) {
this.next()
if (
lineBreak.test(
this.input.slice(this.lastTokEnd, this.start),
)
) {
this.raise(
this.lastTokEnd,
'Illegal newline after throw',
)
}
node.argument = this.parseExpression()
this.semicolon()
return this.finishNode(node, 'ThrowStatement')
}
var empty = []
pp$1.parseTryStatement = function (node) {
this.next()
node.block = this.parseBlock()
node.handler = null
if (this.type === types._catch) {
var clause = this.startNode()
this.next()
this.expect(types.parenL)
clause.param = this.parseBindingAtom()
this.enterLexicalScope()
this.checkLVal(clause.param, 'let')
this.expect(types.parenR)
clause.body = this.parseBlock(false)
this.exitLexicalScope()
node.handler = this.finishNode(
clause,
'CatchClause',
)
}
node.finalizer = this.eat(types._finally)
? this.parseBlock()
: null
if (!node.handler && !node.finalizer) {
this.raise(
node.start,
'Missing catch or finally clause',
)
}
return this.finishNode(node, 'TryStatement')
}
pp$1.parseVarStatement = function (node, kind) {
this.next()
this.parseVar(node, false, kind)
this.semicolon()
return this.finishNode(node, 'VariableDeclaration')
}
pp$1.parseWhileStatement = function (node) {
this.next()
node.test = this.parseParenExpression()
this.labels.push(loopLabel)
node.body = this.parseStatement(false)
this.labels.pop()
return this.finishNode(node, 'WhileStatement')
}
pp$1.parseWithStatement = function (node) {
if (this.strict) {
this.raise(this.start, "'with' in strict mode")
}
this.next()
node.object = this.parseParenExpression()
node.body = this.parseStatement(false)
return this.finishNode(node, 'WithStatement')
}
pp$1.parseEmptyStatement = function (node) {
this.next()
return this.finishNode(node, 'EmptyStatement')
}
pp$1.parseLabeledStatement = function (
node,
maybeName,
expr,
) {
var this$1 = this
for (
var i$1 = 0, list = this$1.labels;
i$1 < list.length;
i$1 += 1
) {
var label = list[i$1]
if (label.name === maybeName) {
this$1.raise(
expr.start,
"Label '" +
maybeName +
"' is already declared",
)
}
}
var kind = this.type.isLoop
? 'loop'
: this.type === types._switch
? 'switch'
: null
for (var i = this.labels.length - 1; i >= 0; i--) {
var label$1 = this$1.labels[i]
if (label$1.statementStart == node.start) {
label$1.statementStart = this$1.start
label$1.kind = kind
} else {
break
}
}
this.labels.push({
name: maybeName,
kind: kind,
statementStart: this.start,
})
node.body = this.parseStatement(true)
if (
node.body.type == 'ClassDeclaration' ||
(node.body.type == 'VariableDeclaration' &&
node.body.kind != 'var') ||
(node.body.type == 'FunctionDeclaration' &&
(this.strict || node.body.generator))
) {
this.raiseRecoverable(
node.body.start,
'Invalid labeled declaration',
)
}
this.labels.pop()
node.label = expr
return this.finishNode(node, 'LabeledStatement')
}
pp$1.parseExpressionStatement = function (node, expr) {
node.expression = expr
this.semicolon()
return this.finishNode(node, 'ExpressionStatement')
}
pp$1.parseBlock = function (createNewLexicalScope) {
var this$1 = this
if (createNewLexicalScope === void 0)
createNewLexicalScope = true
var node = this.startNode()
node.body = []
this.expect(types.braceL)
if (createNewLexicalScope) {
this.enterLexicalScope()
}
while (!this.eat(types.braceR)) {
var stmt = this$1.parseStatement(true)
node.body.push(stmt)
}
if (createNewLexicalScope) {
this.exitLexicalScope()
}
return this.finishNode(node, 'BlockStatement')
}
pp$1.parseFor = function (node, init) {
node.init = init
this.expect(types.semi)
node.test =
this.type === types.semi
? null
: this.parseExpression()
this.expect(types.semi)
node.update =
this.type === types.parenR
? null
: this.parseExpression()
this.expect(types.parenR)
this.exitLexicalScope()
node.body = this.parseStatement(false)
this.labels.pop()
return this.finishNode(node, 'ForStatement')
}
pp$1.parseForIn = function (node, init) {
var type =
this.type === types._in
? 'ForInStatement'
: 'ForOfStatement'
this.next()
node.left = init
node.right = this.parseExpression()
this.expect(types.parenR)
this.exitLexicalScope()
node.body = this.parseStatement(false)
this.labels.pop()
return this.finishNode(node, type)
}
pp$1.parseVar = function (node, isFor, kind) {
var this$1 = this
node.declarations = []
node.kind = kind
for (;;) {
var decl = this$1.startNode()
this$1.parseVarId(decl, kind)
if (this$1.eat(types.eq)) {
decl.init = this$1.parseMaybeAssign(isFor)
} else if (
kind === 'const' &&
!(
this$1.type === types._in ||
(this$1.options.ecmaVersion >= 6 &&
this$1.isContextual('of'))
)
) {
this$1.unexpected()
} else if (
decl.id.type != 'Identifier' &&
!(
isFor &&
(this$1.type === types._in ||
this$1.isContextual('of'))
)
) {
this$1.raise(
this$1.lastTokEnd,
'Complex binding patterns require an initialization value',
)
} else {
decl.init = null
}
node.declarations.push(
this$1.finishNode(decl, 'VariableDeclarator'),
)
if (!this$1.eat(types.comma)) {
break
}
}
return node
}
pp$1.parseVarId = function (decl, kind) {
decl.id = this.parseBindingAtom(kind)
this.checkLVal(decl.id, kind, false)
}
pp$1.parseFunction = function (
node,
isStatement,
allowExpressionBody,
isAsync,
) {
this.initFunction(node)
if (this.options.ecmaVersion >= 6 && !isAsync) {
node.generator = this.eat(types.star)
}
if (this.options.ecmaVersion >= 8) {
node.async = !!isAsync
}
if (isStatement) {
node.id =
isStatement === 'nullableID' &&
this.type != types.name
? null
: this.parseIdent()
if (node.id) {
this.checkLVal(node.id, 'var')
}
}
var oldInGen = this.inGenerator,
oldInAsync = this.inAsync,
oldYieldPos = this.yieldPos,
oldAwaitPos = this.awaitPos,
oldInFunc = this.inFunction
this.inGenerator = node.generator
this.inAsync = node.async
this.yieldPos = 0
this.awaitPos = 0
this.inFunction = true
this.enterFunctionScope()
if (!isStatement) {
node.id =
this.type == types.name
? this.parseIdent()
: null
}
this.parseFunctionParams(node)
this.parseFunctionBody(node, allowExpressionBody)
this.inGenerator = oldInGen
this.inAsync = oldInAsync
this.yieldPos = oldYieldPos
this.awaitPos = oldAwaitPos
this.inFunction = oldInFunc
return this.finishNode(
node,
isStatement
? 'FunctionDeclaration'
: 'FunctionExpression',
)
}
pp$1.parseFunctionParams = function (node) {
this.expect(types.parenL)
node.params = this.parseBindingList(
types.parenR,
false,
this.options.ecmaVersion >= 8,
)
this.checkYieldAwaitInDefaultParams()
}
pp$1.parseClass = function (node, isStatement) {
var this$1 = this
this.next()
this.parseClassId(node, isStatement)
this.parseClassSuper(node)
var classBody = this.startNode()
var hadConstructor = false
classBody.body = []
this.expect(types.braceL)
while (!this.eat(types.braceR)) {
if (this$1.eat(types.semi)) {
continue
}
var method = this$1.startNode()
var isGenerator = this$1.eat(types.star)
var isAsync = false
var isMaybeStatic =
this$1.type === types.name &&
this$1.value === 'static'
this$1.parsePropertyName(method)
method.static =
isMaybeStatic && this$1.type !== types.parenL
if (method.static) {
if (isGenerator) {
this$1.unexpected()
}
isGenerator = this$1.eat(types.star)
this$1.parsePropertyName(method)
}
if (
this$1.options.ecmaVersion >= 8 &&
!isGenerator &&
!method.computed &&
method.key.type === 'Identifier' &&
method.key.name === 'async' &&
this$1.type !== types.parenL &&
!this$1.canInsertSemicolon()
) {
isAsync = true
this$1.parsePropertyName(method)
}
method.kind = 'method'
var isGetSet = false
if (!method.computed) {
var key = method.key
if (
!isGenerator &&
!isAsync &&
key.type === 'Identifier' &&
this$1.type !== types.parenL &&
(key.name === 'get' || key.name === 'set')
) {
isGetSet = true
method.kind = key.name
key = this$1.parsePropertyName(method)
}
if (
!method.static &&
((key.type === 'Identifier' &&
key.name === 'constructor') ||
(key.type === 'Literal' &&
key.value === 'constructor'))
) {
if (hadConstructor) {
this$1.raise(
key.start,
'Duplicate constructor in the same class',
)
}
if (isGetSet) {
this$1.raise(
key.start,
"Constructor can't have get/set modifier",
)
}
if (isGenerator) {
this$1.raise(
key.start,
"Constructor can't be a generator",
)
}
if (isAsync) {
this$1.raise(
key.start,
"Constructor can't be an async method",
)
}
method.kind = 'constructor'
hadConstructor = true
}
}
this$1.parseClassMethod(
classBody,
method,
isGenerator,
isAsync,
)
if (isGetSet) {
var paramCount = method.kind === 'get' ? 0 : 1
if (method.value.params.length !== paramCount) {
var start = method.value.start
if (method.kind === 'get') {
this$1.raiseRecoverable(
start,
'getter should have no params',
)
} else {
this$1.raiseRecoverable(
start,
'setter should have exactly one param',
)
}
} else {
if (
method.kind === 'set' &&
method.value.params[0].type ===
'RestElement'
) {
this$1.raiseRecoverable(
method.value.params[0].start,
'Setter cannot use rest params',
)
}
}
}
}
node.body = this.finishNode(classBody, 'ClassBody')
return this.finishNode(
node,
isStatement
? 'ClassDeclaration'
: 'ClassExpression',
)
}
pp$1.parseClassMethod = function (
classBody,
method,
isGenerator,
isAsync,
) {
method.value = this.parseMethod(isGenerator, isAsync)
classBody.body.push(
this.finishNode(method, 'MethodDefinition'),
)
}
pp$1.parseClassId = function (node, isStatement) {
node.id =
this.type === types.name
? this.parseIdent()
: isStatement === true
? this.unexpected()
: null
}
pp$1.parseClassSuper = function (node) {
node.superClass = this.eat(types._extends)
? this.parseExprSubscripts()
: null
}
pp$1.parseExport = function (node, exports) {
var this$1 = this
this.next()
if (this.eat(types.star)) {
this.expectContextual('from')
node.source =
this.type === types.string
? this.parseExprAtom()
: this.unexpected()
this.semicolon()
return this.finishNode(node, 'ExportAllDeclaration')
}
if (this.eat(types._default)) {
this.checkExport(
exports,
'default',
this.lastTokStart,
)
var isAsync
if (
this.type === types._function ||
(isAsync = this.isAsyncFunction())
) {
var fNode = this.startNode()
this.next()
if (isAsync) {
this.next()
}
node.declaration = this.parseFunction(
fNode,
'nullableID',
false,
isAsync,
)
} else if (this.type === types._class) {
var cNode = this.startNode()
node.declaration = this.parseClass(
cNode,
'nullableID',
)
} else {
node.declaration = this.parseMaybeAssign()
this.semicolon()
}
return this.finishNode(
node,
'ExportDefaultDeclaration',
)
}
if (this.shouldParseExportStatement()) {
node.declaration = this.parseStatement(true)
if (
node.declaration.type === 'VariableDeclaration'
) {
this.checkVariableExport(
exports,
node.declaration.declarations,
)
} else {
this.checkExport(
exports,
node.declaration.id.name,
node.declaration.id.start,
)
}
node.specifiers = []
node.source = null
} else {
node.declaration = null
node.specifiers =
this.parseExportSpecifiers(exports)
if (this.eatContextual('from')) {
node.source =
this.type === types.string
? this.parseExprAtom()
: this.unexpected()
} else {
for (
var i = 0, list = node.specifiers;
i < list.length;
i += 1
) {
var spec = list[i]
this$1.checkUnreserved(spec.local)
}
node.source = null
}
this.semicolon()
}
return this.finishNode(node, 'ExportNamedDeclaration')
}
pp$1.checkExport = function (exports, name, pos) {
if (!exports) {
return
}
if (has(exports, name)) {
this.raiseRecoverable(
pos,
"Duplicate export '" + name + "'",
)
}
exports[name] = true
}
pp$1.checkPatternExport = function (exports, pat) {
var this$1 = this
var type = pat.type
if (type == 'Identifier') {
this.checkExport(exports, pat.name, pat.start)
} else if (type == 'ObjectPattern') {
for (
var i = 0, list = pat.properties;
i < list.length;
i += 1
) {
var prop = list[i]
this$1.checkPatternExport(exports, prop.value)
}
} else if (type == 'ArrayPattern') {
for (
var i$1 = 0, list$1 = pat.elements;
i$1 < list$1.length;
i$1 += 1
) {
var elt = list$1[i$1]
if (elt) {
this$1.checkPatternExport(exports, elt)
}
}
} else if (type == 'AssignmentPattern') {
this.checkPatternExport(exports, pat.left)
} else if (type == 'ParenthesizedExpression') {
this.checkPatternExport(exports, pat.expression)
}
}
pp$1.checkVariableExport = function (exports, decls) {
var this$1 = this
if (!exports) {
return
}
for (var i = 0, list = decls; i < list.length; i += 1) {
var decl = list[i]
this$1.checkPatternExport(exports, decl.id)
}
}
pp$1.shouldParseExportStatement = function () {
return (
this.type.keyword === 'var' ||
this.type.keyword === 'const' ||
this.type.keyword === 'class' ||
this.type.keyword === 'function' ||
this.isLet() ||
this.isAsyncFunction()
)
}
pp$1.parseExportSpecifiers = function (exports) {
var this$1 = this
var nodes = [],
first = true
this.expect(types.braceL)
while (!this.eat(types.braceR)) {
if (!first) {
this$1.expect(types.comma)
if (this$1.afterTrailingComma(types.braceR)) {
break
}
} else {
first = false
}
var node = this$1.startNode()
node.local = this$1.parseIdent(true)
node.exported = this$1.eatContextual('as')
? this$1.parseIdent(true)
: node.local
this$1.checkExport(
exports,
node.exported.name,
node.exported.start,
)
nodes.push(
this$1.finishNode(node, 'ExportSpecifier'),
)
}
return nodes
}
pp$1.parseImport = function (node) {
this.next()
if (this.type === types.string) {
node.specifiers = empty
node.source = this.parseExprAtom()
} else {
node.specifiers = this.parseImportSpecifiers()
this.expectContextual('from')
node.source =
this.type === types.string
? this.parseExprAtom()
: this.unexpected()
}
this.semicolon()
return this.finishNode(node, 'ImportDeclaration')
}
pp$1.parseImportSpecifiers = function () {
var this$1 = this
var nodes = [],
first = true
if (this.type === types.name) {
var node = this.startNode()
node.local = this.parseIdent()
this.checkLVal(node.local, 'let')
nodes.push(
this.finishNode(node, 'ImportDefaultSpecifier'),
)
if (!this.eat(types.comma)) {
return nodes
}
}
if (this.type === types.star) {
var node$1 = this.startNode()
this.next()
this.expectContextual('as')
node$1.local = this.parseIdent()
this.checkLVal(node$1.local, 'let')
nodes.push(
this.finishNode(
node$1,
'ImportNamespaceSpecifier',
),
)
return nodes
}
this.expect(types.braceL)
while (!this.eat(types.braceR)) {
if (!first) {
this$1.expect(types.comma)
if (this$1.afterTrailingComma(types.braceR)) {
break
}
} else {
first = false
}
var node$2 = this$1.startNode()
node$2.imported = this$1.parseIdent(true)
if (this$1.eatContextual('as')) {
node$2.local = this$1.parseIdent()
} else {
this$1.checkUnreserved(node$2.imported)
node$2.local = node$2.imported
}
this$1.checkLVal(node$2.local, 'let')
nodes.push(
this$1.finishNode(node$2, 'ImportSpecifier'),
)
}
return nodes
}
pp$1.adaptDirectivePrologue = function (statements) {
for (
var i = 0;
i < statements.length &&
this.isDirectiveCandidate(statements[i]);
++i
) {
statements[i].directive = statements[
i
].expression.raw.slice(1, -1)
}
}
pp$1.isDirectiveCandidate = function (statement) {
return (
statement.type === 'ExpressionStatement' &&
statement.expression.type === 'Literal' &&
typeof statement.expression.value === 'string' &&
(this.input[statement.start] === '"' ||
this.input[statement.start] === "'")
)
}
var pp$2 = Parser.prototype
pp$2.toAssignable = function (node, isBinding) {
var this$1 = this
if (this.options.ecmaVersion >= 6 && node) {
switch (node.type) {
case 'Identifier':
if (this.inAsync && node.name === 'await') {
this.raise(
node.start,
"Can not use 'await' as identifier inside an async function",
)
}
break
case 'ObjectPattern':
case 'ArrayPattern':
break
case 'ObjectExpression':
node.type = 'ObjectPattern'
for (
var i = 0, list = node.properties;
i < list.length;
i += 1
) {
var prop = list[i]
if (prop.kind !== 'init') {
this$1.raise(
prop.key.start,
"Object pattern can't contain getter or setter",
)
}
this$1.toAssignable(
prop.value,
isBinding,
)
}
break
case 'ArrayExpression':
node.type = 'ArrayPattern'
this.toAssignableList(
node.elements,
isBinding,
)
break
case 'AssignmentExpression':
if (node.operator === '=') {
node.type = 'AssignmentPattern'
delete node.operator
this.toAssignable(node.left, isBinding)
} else {
this.raise(
node.left.end,
"Only '=' operator can be used for specifying default value.",
)
break
}
case 'AssignmentPattern':
break
case 'ParenthesizedExpression':
this.toAssignable(
node.expression,
isBinding,
)
break
case 'MemberExpression':
if (!isBinding) {
break
}
default:
this.raise(
node.start,
'Assigning to rvalue',
)
}
}
return node
}
pp$2.toAssignableList = function (exprList, isBinding) {
var this$1 = this
var end = exprList.length
if (end) {
var last = exprList[end - 1]
if (last && last.type == 'RestElement') {
--end
} else if (last && last.type == 'SpreadElement') {
last.type = 'RestElement'
var arg = last.argument
this.toAssignable(arg, isBinding)
--end
}
if (
this.options.ecmaVersion === 6 &&
isBinding &&
last &&
last.type === 'RestElement' &&
last.argument.type !== 'Identifier'
) {
this.unexpected(last.argument.start)
}
}
for (var i = 0; i < end; i++) {
var elt = exprList[i]
if (elt) {
this$1.toAssignable(elt, isBinding)
}
}
return exprList
}
pp$2.parseSpread = function (refDestructuringErrors) {
var node = this.startNode()
this.next()
node.argument = this.parseMaybeAssign(
false,
refDestructuringErrors,
)
return this.finishNode(node, 'SpreadElement')
}
pp$2.parseRestBinding = function () {
var node = this.startNode()
this.next()
if (
this.options.ecmaVersion === 6 &&
this.type !== types.name
) {
this.unexpected()
}
node.argument = this.parseBindingAtom()
return this.finishNode(node, 'RestElement')
}
pp$2.parseBindingAtom = function () {
if (this.options.ecmaVersion >= 6) {
switch (this.type) {
case types.bracketL:
var node = this.startNode()
this.next()
node.elements = this.parseBindingList(
types.bracketR,
true,
true,
)
return this.finishNode(node, 'ArrayPattern')
case types.braceL:
return this.parseObj(true)
}
}
return this.parseIdent()
}
pp$2.parseBindingList = function (
close,
allowEmpty,
allowTrailingComma,
) {
var this$1 = this
var elts = [],
first = true
while (!this.eat(close)) {
if (first) {
first = false
} else {
this$1.expect(types.comma)
}
if (allowEmpty && this$1.type === types.comma) {
elts.push(null)
} else if (
allowTrailingComma &&
this$1.afterTrailingComma(close)
) {
break
} else if (this$1.type === types.ellipsis) {
var rest = this$1.parseRestBinding()
this$1.parseBindingListItem(rest)
elts.push(rest)
if (this$1.type === types.comma) {
this$1.raise(
this$1.start,
'Comma is not permitted after the rest element',
)
}
this$1.expect(close)
break
} else {
var elem = this$1.parseMaybeDefault(
this$1.start,
this$1.startLoc,
)
this$1.parseBindingListItem(elem)
elts.push(elem)
}
}
return elts
}
pp$2.parseBindingListItem = function (param) {
return param
}
pp$2.parseMaybeDefault = function (
startPos,
startLoc,
left,
) {
left = left || this.parseBindingAtom()
if (
this.options.ecmaVersion < 6 ||
!this.eat(types.eq)
) {
return left
}
var node = this.startNodeAt(startPos, startLoc)
node.left = left
node.right = this.parseMaybeAssign()
return this.finishNode(node, 'AssignmentPattern')
}
pp$2.checkLVal = function (
expr,
bindingType,
checkClashes,
) {
var this$1 = this
switch (expr.type) {
case 'Identifier':
if (
this.strict &&
this.reservedWordsStrictBind.test(expr.name)
) {
this.raiseRecoverable(
expr.start,
(bindingType
? 'Binding '
: 'Assigning to ') +
expr.name +
' in strict mode',
)
}
if (checkClashes) {
if (has(checkClashes, expr.name)) {
this.raiseRecoverable(
expr.start,
'Argument name clash',
)
}
checkClashes[expr.name] = true
}
if (bindingType && bindingType !== 'none') {
if (
(bindingType === 'var' &&
!this.canDeclareVarName(
expr.name,
)) ||
(bindingType !== 'var' &&
!this.canDeclareLexicalName(
expr.name,
))
) {
this.raiseRecoverable(
expr.start,
"Identifier '" +
expr.name +
"' has already been declared",
)
}
if (bindingType === 'var') {
this.declareVarName(expr.name)
} else {
this.declareLexicalName(expr.name)
}
}
break
case 'MemberExpression':
if (bindingType) {
this.raiseRecoverable(
expr.start,
(bindingType
? 'Binding'
: 'Assigning to') +
' member expression',
)
}
break
case 'ObjectPattern':
for (
var i = 0, list = expr.properties;
i < list.length;
i += 1
) {
var prop = list[i]
this$1.checkLVal(
prop.value,
bindingType,
checkClashes,
)
}
break
case 'ArrayPattern':
for (
var i$1 = 0, list$1 = expr.elements;
i$1 < list$1.length;
i$1 += 1
) {
var elem = list$1[i$1]
if (elem) {
this$1.checkLVal(
elem,
bindingType,
checkClashes,
)
}
}
break
case 'AssignmentPattern':
this.checkLVal(
expr.left,
bindingType,
checkClashes,
)
break
case 'RestElement':
this.checkLVal(
expr.argument,
bindingType,
checkClashes,
)
break
case 'ParenthesizedExpression':
this.checkLVal(
expr.expression,
bindingType,
checkClashes,
)
break
default:
this.raise(
expr.start,
(bindingType ? 'Binding' : 'Assigning to') +
' rvalue',
)
}
}
var pp$3 = Parser.prototype
pp$3.checkPropClash = function (prop, propHash) {
if (
this.options.ecmaVersion >= 6 &&
(prop.computed || prop.method || prop.shorthand)
) {
return
}
var key = prop.key
var name
switch (key.type) {
case 'Identifier':
name = key.name
break
case 'Literal':
name = String(key.value)
break
default:
return
}
var kind = prop.kind
if (this.options.ecmaVersion >= 6) {
if (name === '__proto__' && kind === 'init') {
if (propHash.proto) {
this.raiseRecoverable(
key.start,
'Redefinition of __proto__ property',
)
}
propHash.proto = true
}
return
}
name = '$' + name
var other = propHash[name]
if (other) {
var redefinition
if (kind === 'init') {
redefinition =
(this.strict && other.init) ||
other.get ||
other.set
} else {
redefinition = other.init || other[kind]
}
if (redefinition) {
this.raiseRecoverable(
key.start,
'Redefinition of property',
)
}
} else {
other = propHash[name] = {
init: false,
get: false,
set: false,
}
}
other[kind] = true
}
pp$3.parseExpression = function (
noIn,
refDestructuringErrors,
) {
var this$1 = this
var startPos = this.start,
startLoc = this.startLoc
var expr = this.parseMaybeAssign(
noIn,
refDestructuringErrors,
)
if (this.type === types.comma) {
var node = this.startNodeAt(startPos, startLoc)
node.expressions = [expr]
while (this.eat(types.comma)) {
node.expressions.push(
this$1.parseMaybeAssign(
noIn,
refDestructuringErrors,
),
)
}
return this.finishNode(node, 'SequenceExpression')
}
return expr
}
pp$3.parseMaybeAssign = function (
noIn,
refDestructuringErrors,
afterLeftParse,
) {
if (this.inGenerator && this.isContextual('yield')) {
return this.parseYield()
}
var ownDestructuringErrors = false,
oldParenAssign = -1,
oldTrailingComma = -1
if (refDestructuringErrors) {
oldParenAssign =
refDestructuringErrors.parenthesizedAssign
oldTrailingComma =
refDestructuringErrors.trailingComma
refDestructuringErrors.parenthesizedAssign =
refDestructuringErrors.trailingComma = -1
} else {
refDestructuringErrors = new DestructuringErrors()
ownDestructuringErrors = true
}
var startPos = this.start,
startLoc = this.startLoc
if (
this.type == types.parenL ||
this.type == types.name
) {
this.potentialArrowAt = this.start
}
var left = this.parseMaybeConditional(
noIn,
refDestructuringErrors,
)
if (afterLeftParse) {
left = afterLeftParse.call(
this,
left,
startPos,
startLoc,
)
}
if (this.type.isAssign) {
this.checkPatternErrors(
refDestructuringErrors,
true,
)
if (!ownDestructuringErrors) {
DestructuringErrors.call(refDestructuringErrors)
}
var node = this.startNodeAt(startPos, startLoc)
node.operator = this.value
node.left =
this.type === types.eq
? this.toAssignable(left)
: left
refDestructuringErrors.shorthandAssign = -1
this.checkLVal(left)
this.next()
node.right = this.parseMaybeAssign(noIn)
return this.finishNode(node, 'AssignmentExpression')
} else {
if (ownDestructuringErrors) {
this.checkExpressionErrors(
refDestructuringErrors,
true,
)
}
}
if (oldParenAssign > -1) {
refDestructuringErrors.parenthesizedAssign =
oldParenAssign
}
if (oldTrailingComma > -1) {
refDestructuringErrors.trailingComma =
oldTrailingComma
}
return left
}
pp$3.parseMaybeConditional = function (
noIn,
refDestructuringErrors,
) {
var startPos = this.start,
startLoc = this.startLoc
var expr = this.parseExprOps(
noIn,
refDestructuringErrors,
)
if (
this.checkExpressionErrors(refDestructuringErrors)
) {
return expr
}
if (this.eat(types.question)) {
var node = this.startNodeAt(startPos, startLoc)
node.test = expr
node.consequent = this.parseMaybeAssign()
this.expect(types.colon)
node.alternate = this.parseMaybeAssign(noIn)
return this.finishNode(
node,
'ConditionalExpression',
)
}
return expr
}
pp$3.parseExprOps = function (
noIn,
refDestructuringErrors,
) {
var startPos = this.start,
startLoc = this.startLoc
var expr = this.parseMaybeUnary(
refDestructuringErrors,
false,
)
if (
this.checkExpressionErrors(refDestructuringErrors)
) {
return expr
}
return expr.start == startPos &&
expr.type === 'ArrowFunctionExpression'
? expr
: this.parseExprOp(
expr,
startPos,
startLoc,
-1,
noIn,
)
}
pp$3.parseExprOp = function (
left,
leftStartPos,
leftStartLoc,
minPrec,
noIn,
) {
var prec = this.type.binop
if (
prec != null &&
(!noIn || this.type !== types._in)
) {
if (prec > minPrec) {
var logical =
this.type === types.logicalOR ||
this.type === types.logicalAND
var op = this.value
this.next()
var startPos = this.start,
startLoc = this.startLoc
var right = this.parseExprOp(
this.parseMaybeUnary(null, false),
startPos,
startLoc,
prec,
noIn,
)
var node = this.buildBinary(
leftStartPos,
leftStartLoc,
left,
right,
op,
logical,
)
return this.parseExprOp(
node,
leftStartPos,
leftStartLoc,
minPrec,
noIn,
)
}
}
return left
}
pp$3.buildBinary = function (
startPos,
startLoc,
left,
right,
op,
logical,
) {
var node = this.startNodeAt(startPos, startLoc)
node.left = left
node.operator = op
node.right = right
return this.finishNode(
node,
logical ? 'LogicalExpression' : 'BinaryExpression',
)
}
pp$3.parseMaybeUnary = function (
refDestructuringErrors,
sawUnary,
) {
var this$1 = this
var startPos = this.start,
startLoc = this.startLoc,
expr
if (this.inAsync && this.isContextual('await')) {
expr = this.parseAwait()
sawUnary = true
} else if (this.type.prefix) {
var node = this.startNode(),
update = this.type === types.incDec
node.operator = this.value
node.prefix = true
this.next()
node.argument = this.parseMaybeUnary(null, true)
this.checkExpressionErrors(
refDestructuringErrors,
true,
)
if (update) {
this.checkLVal(node.argument)
} else if (
this.strict &&
node.operator === 'delete' &&
node.argument.type === 'Identifier'
) {
this.raiseRecoverable(
node.start,
'Deleting local variable in strict mode',
)
} else {
sawUnary = true
}
expr = this.finishNode(
node,
update ? 'UpdateExpression' : 'UnaryExpression',
)
} else {
expr = this.parseExprSubscripts(
refDestructuringErrors,
)
if (
this.checkExpressionErrors(
refDestructuringErrors,
)
) {
return expr
}
while (
this.type.postfix &&
!this.canInsertSemicolon()
) {
var node$1 = this$1.startNodeAt(
startPos,
startLoc,
)
node$1.operator = this$1.value
node$1.prefix = false
node$1.argument = expr
this$1.checkLVal(expr)
this$1.next()
expr = this$1.finishNode(
node$1,
'UpdateExpression',
)
}
}
if (!sawUnary && this.eat(types.starstar)) {
return this.buildBinary(
startPos,
startLoc,
expr,
this.parseMaybeUnary(null, false),
'**',
false,
)
} else {
return expr
}
}
pp$3.parseExprSubscripts = function (
refDestructuringErrors,
) {
var startPos = this.start,
startLoc = this.startLoc
var expr = this.parseExprAtom(refDestructuringErrors)
var skipArrowSubscripts =
expr.type === 'ArrowFunctionExpression' &&
this.input.slice(
this.lastTokStart,
this.lastTokEnd,
) !== ')'
if (
this.checkExpressionErrors(
refDestructuringErrors,
) ||
skipArrowSubscripts
) {
return expr
}
var result = this.parseSubscripts(
expr,
startPos,
startLoc,
)
if (
refDestructuringErrors &&
result.type === 'MemberExpression'
) {
if (
refDestructuringErrors.parenthesizedAssign >=
result.start
) {
refDestructuringErrors.parenthesizedAssign = -1
}
if (
refDestructuringErrors.parenthesizedBind >=
result.start
) {
refDestructuringErrors.parenthesizedBind = -1
}
}
return result
}
pp$3.parseSubscripts = function (
base,
startPos,
startLoc,
noCalls,
) {
var this$1 = this
var maybeAsyncArrow =
this.options.ecmaVersion >= 8 &&
base.type === 'Identifier' &&
base.name === 'async' &&
this.lastTokEnd == base.end &&
!this.canInsertSemicolon()
for (var computed = void 0; ; ) {
if (
(computed = this$1.eat(types.bracketL)) ||
this$1.eat(types.dot)
) {
var node = this$1.startNodeAt(
startPos,
startLoc,
)
node.object = base
node.property = computed
? this$1.parseExpression()
: this$1.parseIdent(true)
node.computed = !!computed
if (computed) {
this$1.expect(types.bracketR)
}
base = this$1.finishNode(
node,
'MemberExpression',
)
} else if (!noCalls && this$1.eat(types.parenL)) {
var refDestructuringErrors =
new DestructuringErrors(),
oldYieldPos = this$1.yieldPos,
oldAwaitPos = this$1.awaitPos
this$1.yieldPos = 0
this$1.awaitPos = 0
var exprList = this$1.parseExprList(
types.parenR,
this$1.options.ecmaVersion >= 8,
false,
refDestructuringErrors,
)
if (
maybeAsyncArrow &&
!this$1.canInsertSemicolon() &&
this$1.eat(types.arrow)
) {
this$1.checkPatternErrors(
refDestructuringErrors,
false,
)
this$1.checkYieldAwaitInDefaultParams()
this$1.yieldPos = oldYieldPos
this$1.awaitPos = oldAwaitPos
return this$1.parseArrowExpression(
this$1.startNodeAt(startPos, startLoc),
exprList,
true,
)
}
this$1.checkExpressionErrors(
refDestructuringErrors,
true,
)
this$1.yieldPos = oldYieldPos || this$1.yieldPos
this$1.awaitPos = oldAwaitPos || this$1.awaitPos
var node$1 = this$1.startNodeAt(
startPos,
startLoc,
)
node$1.callee = base
node$1.arguments = exprList
base = this$1.finishNode(
node$1,
'CallExpression',
)
} else if (this$1.type === types.backQuote) {
var node$2 = this$1.startNodeAt(
startPos,
startLoc,
)
node$2.tag = base
node$2.quasi = this$1.parseTemplate({
isTagged: true,
})
base = this$1.finishNode(
node$2,
'TaggedTemplateExpression',
)
} else {
return base
}
}
}
pp$3.parseExprAtom = function (refDestructuringErrors) {
var node,
canBeArrow = this.potentialArrowAt == this.start
switch (this.type) {
case types._super:
if (!this.inFunction) {
this.raise(
this.start,
"'super' outside of function or class",
)
}
node = this.startNode()
this.next()
if (
this.type !== types.dot &&
this.type !== types.bracketL &&
this.type !== types.parenL
) {
this.unexpected()
}
return this.finishNode(node, 'Super')
case types._this:
node = this.startNode()
this.next()
return this.finishNode(node, 'ThisExpression')
case types.name:
var startPos = this.start,
startLoc = this.startLoc
var id = this.parseIdent(
this.type !== types.name,
)
if (
this.options.ecmaVersion >= 8 &&
id.name === 'async' &&
!this.canInsertSemicolon() &&
this.eat(types._function)
) {
return this.parseFunction(
this.startNodeAt(startPos, startLoc),
false,
false,
true,
)
}
if (canBeArrow && !this.canInsertSemicolon()) {
if (this.eat(types.arrow)) {
return this.parseArrowExpression(
this.startNodeAt(
startPos,
startLoc,
),
[id],
false,
)
}
if (
this.options.ecmaVersion >= 8 &&
id.name === 'async' &&
this.type === types.name
) {
id = this.parseIdent()
if (
this.canInsertSemicolon() ||
!this.eat(types.arrow)
) {
this.unexpected()
}
return this.parseArrowExpression(
this.startNodeAt(
startPos,
startLoc,
),
[id],
true,
)
}
}
return id
case types.regexp:
var value = this.value
node = this.parseLiteral(value.value)
node.regex = {
pattern: value.pattern,
flags: value.flags,
}
return node
case types.num:
case types.string:
return this.parseLiteral(this.value)
case types._null:
case types._true:
case types._false:
node = this.startNode()
node.value =
this.type === types._null
? null
: this.type === types._true
node.raw = this.type.keyword
this.next()
return this.finishNode(node, 'Literal')
case types.parenL:
var start = this.start,
expr =
this.parseParenAndDistinguishExpression(
canBeArrow,
)
if (refDestructuringErrors) {
if (
refDestructuringErrors.parenthesizedAssign <
0 &&
!this.isSimpleAssignTarget(expr)
) {
refDestructuringErrors.parenthesizedAssign =
start
}
if (
refDestructuringErrors.parenthesizedBind <
0
) {
refDestructuringErrors.parenthesizedBind =
start
}
}
return expr
case types.bracketL:
node = this.startNode()
this.next()
node.elements = this.parseExprList(
types.bracketR,
true,
true,
refDestructuringErrors,
)
return this.finishNode(node, 'ArrayExpression')
case types.braceL:
return this.parseObj(
false,
refDestructuringErrors,
)
case types._function:
node = this.startNode()
this.next()
return this.parseFunction(node, false)
case types._class:
return this.parseClass(this.startNode(), false)
case types._new:
return this.parseNew()
case types.backQuote:
return this.parseTemplate()
default:
this.unexpected()
}
}
pp$3.parseLiteral = function (value) {
var node = this.startNode()
node.value = value
node.raw = this.input.slice(this.start, this.end)
this.next()
return this.finishNode(node, 'Literal')
}
pp$3.parseParenExpression = function () {
this.expect(types.parenL)
var val = this.parseExpression()
this.expect(types.parenR)
return val
}
pp$3.parseParenAndDistinguishExpression = function (
canBeArrow,
) {
var this$1 = this
var startPos = this.start,
startLoc = this.startLoc,
val,
allowTrailingComma = this.options.ecmaVersion >= 8
if (this.options.ecmaVersion >= 6) {
this.next()
var innerStartPos = this.start,
innerStartLoc = this.startLoc
var exprList = [],
first = true,
lastIsComma = false
var refDestructuringErrors =
new DestructuringErrors(),
oldYieldPos = this.yieldPos,
oldAwaitPos = this.awaitPos,
spreadStart,
innerParenStart
this.yieldPos = 0
this.awaitPos = 0
while (this.type !== types.parenR) {
first
? (first = false)
: this$1.expect(types.comma)
if (
allowTrailingComma &&
this$1.afterTrailingComma(
types.parenR,
true,
)
) {
lastIsComma = true
break
} else if (this$1.type === types.ellipsis) {
spreadStart = this$1.start
exprList.push(
this$1.parseParenItem(
this$1.parseRestBinding(),
),
)
if (this$1.type === types.comma) {
this$1.raise(
this$1.start,
'Comma is not permitted after the rest element',
)
}
break
} else {
if (
this$1.type === types.parenL &&
!innerParenStart
) {
innerParenStart = this$1.start
}
exprList.push(
this$1.parseMaybeAssign(
false,
refDestructuringErrors,
this$1.parseParenItem,
),
)
}
}
var innerEndPos = this.start,
innerEndLoc = this.startLoc
this.expect(types.parenR)
if (
canBeArrow &&
!this.canInsertSemicolon() &&
this.eat(types.arrow)
) {
this.checkPatternErrors(
refDestructuringErrors,
false,
)
this.checkYieldAwaitInDefaultParams()
if (innerParenStart) {
this.unexpected(innerParenStart)
}
this.yieldPos = oldYieldPos
this.awaitPos = oldAwaitPos
return this.parseParenArrowList(
startPos,
startLoc,
exprList,
)
}
if (!exprList.length || lastIsComma) {
this.unexpected(this.lastTokStart)
}
if (spreadStart) {
this.unexpected(spreadStart)
}
this.checkExpressionErrors(
refDestructuringErrors,
true,
)
this.yieldPos = oldYieldPos || this.yieldPos
this.awaitPos = oldAwaitPos || this.awaitPos
if (exprList.length > 1) {
val = this.startNodeAt(
innerStartPos,
innerStartLoc,
)
val.expressions = exprList
this.finishNodeAt(
val,
'SequenceExpression',
innerEndPos,
innerEndLoc,
)
} else {
val = exprList[0]
}
} else {
val = this.parseParenExpression()
}
if (this.options.preserveParens) {
var par = this.startNodeAt(startPos, startLoc)
par.expression = val
return this.finishNode(
par,
'ParenthesizedExpression',
)
} else {
return val
}
}
pp$3.parseParenItem = function (item) {
return item
}
pp$3.parseParenArrowList = function (
startPos,
startLoc,
exprList,
) {
return this.parseArrowExpression(
this.startNodeAt(startPos, startLoc),
exprList,
)
}
var empty$1 = []
pp$3.parseNew = function () {
var node = this.startNode()
var meta = this.parseIdent(true)
if (
this.options.ecmaVersion >= 6 &&
this.eat(types.dot)
) {
node.meta = meta
node.property = this.parseIdent(true)
if (node.property.name !== 'target') {
this.raiseRecoverable(
node.property.start,
'The only valid meta property for new is new.target',
)
}
if (!this.inFunction) {
this.raiseRecoverable(
node.start,
'new.target can only be used in functions',
)
}
return this.finishNode(node, 'MetaProperty')
}
var startPos = this.start,
startLoc = this.startLoc
node.callee = this.parseSubscripts(
this.parseExprAtom(),
startPos,
startLoc,
true,
)
if (this.eat(types.parenL)) {
node.arguments = this.parseExprList(
types.parenR,
this.options.ecmaVersion >= 8,
false,
)
} else {
node.arguments = empty$1
}
return this.finishNode(node, 'NewExpression')
}
pp$3.parseTemplateElement = function (ref) {
var isTagged = ref.isTagged
var elem = this.startNode()
if (this.type === types.invalidTemplate) {
if (!isTagged) {
this.raiseRecoverable(
this.start,
'Bad escape sequence in untagged template literal',
)
}
elem.value = {
raw: this.value,
cooked: null,
}
} else {
elem.value = {
raw: this.input
.slice(this.start, this.end)
.replace(/\r\n?/g, '\n'),
cooked: this.value,
}
}
this.next()
elem.tail = this.type === types.backQuote
return this.finishNode(elem, 'TemplateElement')
}
pp$3.parseTemplate = function (ref) {
var this$1 = this
if (ref === void 0) ref = {}
var isTagged = ref.isTagged
if (isTagged === void 0) isTagged = false
var node = this.startNode()
this.next()
node.expressions = []
var curElt = this.parseTemplateElement({
isTagged: isTagged,
})
node.quasis = [curElt]
while (!curElt.tail) {
this$1.expect(types.dollarBraceL)
node.expressions.push(this$1.parseExpression())
this$1.expect(types.braceR)
node.quasis.push(
(curElt = this$1.parseTemplateElement({
isTagged: isTagged,
})),
)
}
this.next()
return this.finishNode(node, 'TemplateLiteral')
}
pp$3.isAsyncProp = function (prop) {
return (
!prop.computed &&
prop.key.type === 'Identifier' &&
prop.key.name === 'async' &&
(this.type === types.name ||
this.type === types.num ||
this.type === types.string ||
this.type === types.bracketL ||
this.type.keyword) &&
!lineBreak.test(
this.input.slice(this.lastTokEnd, this.start),
)
)
}
pp$3.parseObj = function (
isPattern,
refDestructuringErrors,
) {
var this$1 = this
var node = this.startNode(),
first = true,
propHash = {}
node.properties = []
this.next()
while (!this.eat(types.braceR)) {
if (!first) {
this$1.expect(types.comma)
if (this$1.afterTrailingComma(types.braceR)) {
break
}
} else {
first = false
}
var prop = this$1.parseProperty(
isPattern,
refDestructuringErrors,
)
this$1.checkPropClash(prop, propHash)
node.properties.push(prop)
}
return this.finishNode(
node,
isPattern ? 'ObjectPattern' : 'ObjectExpression',
)
}
pp$3.parseProperty = function (
isPattern,
refDestructuringErrors,
) {
var prop = this.startNode(),
isGenerator,
isAsync,
startPos,
startLoc
if (this.options.ecmaVersion >= 6) {
prop.method = false
prop.shorthand = false
if (isPattern || refDestructuringErrors) {
startPos = this.start
startLoc = this.startLoc
}
if (!isPattern) {
isGenerator = this.eat(types.star)
}
}
this.parsePropertyName(prop)
if (
!isPattern &&
this.options.ecmaVersion >= 8 &&
!isGenerator &&
this.isAsyncProp(prop)
) {
isAsync = true
this.parsePropertyName(prop, refDestructuringErrors)
} else {
isAsync = false
}
this.parsePropertyValue(
prop,
isPattern,
isGenerator,
isAsync,
startPos,
startLoc,
refDestructuringErrors,
)
return this.finishNode(prop, 'Property')
}
pp$3.parsePropertyValue = function (
prop,
isPattern,
isGenerator,
isAsync,
startPos,
startLoc,
refDestructuringErrors,
) {
if (
(isGenerator || isAsync) &&
this.type === types.colon
) {
this.unexpected()
}
if (this.eat(types.colon)) {
prop.value = isPattern
? this.parseMaybeDefault(
this.start,
this.startLoc,
)
: this.parseMaybeAssign(
false,
refDestructuringErrors,
)
prop.kind = 'init'
} else if (
this.options.ecmaVersion >= 6 &&
this.type === types.parenL
) {
if (isPattern) {
this.unexpected()
}
prop.kind = 'init'
prop.method = true
prop.value = this.parseMethod(isGenerator, isAsync)
} else if (
!isPattern &&
this.options.ecmaVersion >= 5 &&
!prop.computed &&
prop.key.type === 'Identifier' &&
(prop.key.name === 'get' ||
prop.key.name === 'set') &&
this.type != types.comma &&
this.type != types.braceR
) {
if (isGenerator || isAsync) {
this.unexpected()
}
prop.kind = prop.key.name
this.parsePropertyName(prop)
prop.value = this.parseMethod(false)
var paramCount = prop.kind === 'get' ? 0 : 1
if (prop.value.params.length !== paramCount) {
var start = prop.value.start
if (prop.kind === 'get') {
this.raiseRecoverable(
start,
'getter should have no params',
)
} else {
this.raiseRecoverable(
start,
'setter should have exactly one param',
)
}
} else {
if (
prop.kind === 'set' &&
prop.value.params[0].type === 'RestElement'
) {
this.raiseRecoverable(
prop.value.params[0].start,
'Setter cannot use rest params',
)
}
}
} else if (
this.options.ecmaVersion >= 6 &&
!prop.computed &&
prop.key.type === 'Identifier'
) {
this.checkUnreserved(prop.key)
prop.kind = 'init'
if (isPattern) {
prop.value = this.parseMaybeDefault(
startPos,
startLoc,
prop.key,
)
} else if (
this.type === types.eq &&
refDestructuringErrors
) {
if (
refDestructuringErrors.shorthandAssign < 0
) {
refDestructuringErrors.shorthandAssign =
this.start
}
prop.value = this.parseMaybeDefault(
startPos,
startLoc,
prop.key,
)
} else {
prop.value = prop.key
}
prop.shorthand = true
} else {
this.unexpected()
}
}
pp$3.parsePropertyName = function (prop) {
if (this.options.ecmaVersion >= 6) {
if (this.eat(types.bracketL)) {
prop.computed = true
prop.key = this.parseMaybeAssign()
this.expect(types.bracketR)
return prop.key
} else {
prop.computed = false
}
}
return (prop.key =
this.type === types.num ||
this.type === types.string
? this.parseExprAtom()
: this.parseIdent(true))
}
pp$3.initFunction = function (node) {
node.id = null
if (this.options.ecmaVersion >= 6) {
node.generator = false
node.expression = false
}
if (this.options.ecmaVersion >= 8) {
node.async = false
}
}
pp$3.parseMethod = function (isGenerator, isAsync) {
var node = this.startNode(),
oldInGen = this.inGenerator,
oldInAsync = this.inAsync,
oldYieldPos = this.yieldPos,
oldAwaitPos = this.awaitPos,
oldInFunc = this.inFunction
this.initFunction(node)
if (this.options.ecmaVersion >= 6) {
node.generator = isGenerator
}
if (this.options.ecmaVersion >= 8) {
node.async = !!isAsync
}
this.inGenerator = node.generator
this.inAsync = node.async
this.yieldPos = 0
this.awaitPos = 0
this.inFunction = true
this.enterFunctionScope()
this.expect(types.parenL)
node.params = this.parseBindingList(
types.parenR,
false,
this.options.ecmaVersion >= 8,
)
this.checkYieldAwaitInDefaultParams()
this.parseFunctionBody(node, false)
this.inGenerator = oldInGen
this.inAsync = oldInAsync
this.yieldPos = oldYieldPos
this.awaitPos = oldAwaitPos
this.inFunction = oldInFunc
return this.finishNode(node, 'FunctionExpression')
}
pp$3.parseArrowExpression = function (
node,
params,
isAsync,
) {
var oldInGen = this.inGenerator,
oldInAsync = this.inAsync,
oldYieldPos = this.yieldPos,
oldAwaitPos = this.awaitPos,
oldInFunc = this.inFunction
this.enterFunctionScope()
this.initFunction(node)
if (this.options.ecmaVersion >= 8) {
node.async = !!isAsync
}
this.inGenerator = false
this.inAsync = node.async
this.yieldPos = 0
this.awaitPos = 0
this.inFunction = true
node.params = this.toAssignableList(params, true)
this.parseFunctionBody(node, true)
this.inGenerator = oldInGen
this.inAsync = oldInAsync
this.yieldPos = oldYieldPos
this.awaitPos = oldAwaitPos
this.inFunction = oldInFunc
return this.finishNode(node, 'ArrowFunctionExpression')
}
pp$3.parseFunctionBody = function (node, isArrowFunction) {
var isExpression =
isArrowFunction && this.type !== types.braceL
var oldStrict = this.strict,
useStrict = false
if (isExpression) {
node.body = this.parseMaybeAssign()
node.expression = true
this.checkParams(node, false)
} else {
var nonSimple =
this.options.ecmaVersion >= 7 &&
!this.isSimpleParamList(node.params)
if (!oldStrict || nonSimple) {
useStrict = this.strictDirective(this.end)
if (useStrict && nonSimple) {
this.raiseRecoverable(
node.start,
"Illegal 'use strict' directive in function with non-simple parameter list",
)
}
}
var oldLabels = this.labels
this.labels = []
if (useStrict) {
this.strict = true
}
this.checkParams(
node,
!oldStrict &&
!useStrict &&
!isArrowFunction &&
this.isSimpleParamList(node.params),
)
node.body = this.parseBlock(false)
node.expression = false
this.adaptDirectivePrologue(node.body.body)
this.labels = oldLabels
}
this.exitFunctionScope()
if (this.strict && node.id) {
this.checkLVal(node.id, 'none')
}
this.strict = oldStrict
}
pp$3.isSimpleParamList = function (params) {
for (
var i = 0, list = params;
i < list.length;
i += 1
) {
var param = list[i]
if (param.type !== 'Identifier') {
return false
}
}
return true
}
pp$3.checkParams = function (node, allowDuplicates) {
var this$1 = this
var nameHash = {}
for (
var i = 0, list = node.params;
i < list.length;
i += 1
) {
var param = list[i]
this$1.checkLVal(
param,
'var',
allowDuplicates ? null : nameHash,
)
}
}
pp$3.parseExprList = function (
close,
allowTrailingComma,
allowEmpty,
refDestructuringErrors,
) {
var this$1 = this
var elts = [],
first = true
while (!this.eat(close)) {
if (!first) {
this$1.expect(types.comma)
if (
allowTrailingComma &&
this$1.afterTrailingComma(close)
) {
break
}
} else {
first = false
}
var elt = void 0
if (allowEmpty && this$1.type === types.comma) {
elt = null
} else if (this$1.type === types.ellipsis) {
elt = this$1.parseSpread(refDestructuringErrors)
if (
refDestructuringErrors &&
this$1.type === types.comma &&
refDestructuringErrors.trailingComma < 0
) {
refDestructuringErrors.trailingComma =
this$1.start
}
} else {
elt = this$1.parseMaybeAssign(
false,
refDestructuringErrors,
)
}
elts.push(elt)
}
return elts
}
pp$3.checkUnreserved = function (ref) {
var start = ref.start
var end = ref.end
var name = ref.name
if (this.inGenerator && name === 'yield') {
this.raiseRecoverable(
start,
"Can not use 'yield' as identifier inside a generator",
)
}
if (this.inAsync && name === 'await') {
this.raiseRecoverable(
start,
"Can not use 'await' as identifier inside an async function",
)
}
if (this.isKeyword(name)) {
this.raise(
start,
"Unexpected keyword '" + name + "'",
)
}
if (
this.options.ecmaVersion < 6 &&
this.input.slice(start, end).indexOf('\\') != -1
) {
return
}
var re = this.strict
? this.reservedWordsStrict
: this.reservedWords
if (re.test(name)) {
this.raiseRecoverable(
start,
"The keyword '" + name + "' is reserved",
)
}
}
pp$3.parseIdent = function (liberal, isBinding) {
var node = this.startNode()
if (liberal && this.options.allowReserved == 'never') {
liberal = false
}
if (this.type === types.name) {
node.name = this.value
} else if (this.type.keyword) {
node.name = this.type.keyword
if (
(node.name === 'class' ||
node.name === 'function') &&
(this.lastTokEnd !== this.lastTokStart + 1 ||
this.input.charCodeAt(this.lastTokStart) !==
46)
) {
this.context.pop()
}
} else {
this.unexpected()
}
this.next()
this.finishNode(node, 'Identifier')
if (!liberal) {
this.checkUnreserved(node)
}
return node
}
pp$3.parseYield = function () {
if (!this.yieldPos) {
this.yieldPos = this.start
}
var node = this.startNode()
this.next()
if (
this.type == types.semi ||
this.canInsertSemicolon() ||
(this.type != types.star && !this.type.startsExpr)
) {
node.delegate = false
node.argument = null
} else {
node.delegate = this.eat(types.star)
node.argument = this.parseMaybeAssign()
}
return this.finishNode(node, 'YieldExpression')
}
pp$3.parseAwait = function () {
if (!this.awaitPos) {
this.awaitPos = this.start
}
var node = this.startNode()
this.next()
node.argument = this.parseMaybeUnary(null, true)
return this.finishNode(node, 'AwaitExpression')
}
var pp$4 = Parser.prototype
pp$4.raise = function (pos, message) {
var loc = getLineInfo(this.input, pos)
message += ' (' + loc.line + ':' + loc.column + ')'
var err = new SyntaxError(message)
err.pos = pos
err.loc = loc
err.raisedAt = this.pos
throw err
}
pp$4.raiseRecoverable = pp$4.raise
pp$4.curPosition = function () {
if (this.options.locations) {
return new Position(
this.curLine,
this.pos - this.lineStart,
)
}
}
var pp$5 = Parser.prototype
var assign =
Object.assign ||
function (target) {
var sources = [],
len = arguments.length - 1
while (len-- > 0) sources[len] = arguments[len + 1]
for (
var i = 0, list = sources;
i < list.length;
i += 1
) {
var source = list[i]
for (var key in source) {
if (has(source, key)) {
target[key] = source[key]
}
}
}
return target
}
pp$5.enterFunctionScope = function () {
this.scopeStack.push({
var: {},
lexical: {},
childVar: {},
parentLexical: {},
})
}
pp$5.exitFunctionScope = function () {
this.scopeStack.pop()
}
pp$5.enterLexicalScope = function () {
var parentScope =
this.scopeStack[this.scopeStack.length - 1]
var childScope = {
var: {},
lexical: {},
childVar: {},
parentLexical: {},
}
this.scopeStack.push(childScope)
assign(
childScope.parentLexical,
parentScope.lexical,
parentScope.parentLexical,
)
}
pp$5.exitLexicalScope = function () {
var childScope = this.scopeStack.pop()
var parentScope =
this.scopeStack[this.scopeStack.length - 1]
assign(
parentScope.childVar,
childScope.var,
childScope.childVar,
)
}
pp$5.canDeclareVarName = function (name) {
var currentScope =
this.scopeStack[this.scopeStack.length - 1]
return (
!has(currentScope.lexical, name) &&
!has(currentScope.parentLexical, name)
)
}
pp$5.canDeclareLexicalName = function (name) {
var currentScope =
this.scopeStack[this.scopeStack.length - 1]
return (
!has(currentScope.lexical, name) &&
!has(currentScope.var, name) &&
!has(currentScope.childVar, name)
)
}
pp$5.declareVarName = function (name) {
this.scopeStack[this.scopeStack.length - 1].var[
name
] = true
}
pp$5.declareLexicalName = function (name) {
this.scopeStack[this.scopeStack.length - 1].lexical[
name
] = true
}
var Node = function Node(parser, pos, loc) {
this.type = ''
this.start = pos
this.end = 0
if (parser.options.locations) {
this.loc = new SourceLocation(parser, loc)
}
if (parser.options.directSourceFile) {
this.sourceFile = parser.options.directSourceFile
}
if (parser.options.ranges) {
this.range = [pos, 0]
}
}
var pp$6 = Parser.prototype
pp$6.startNode = function () {
return new Node(this, this.start, this.startLoc)
}
pp$6.startNodeAt = function (pos, loc) {
return new Node(this, pos, loc)
}
function finishNodeAt(node, type, pos, loc) {
node.type = type
node.end = pos
if (this.options.locations) {
node.loc.end = loc
}
if (this.options.ranges) {
node.range[1] = pos
}
return node
}
pp$6.finishNode = function (node, type) {
return finishNodeAt.call(
this,
node,
type,
this.lastTokEnd,
this.lastTokEndLoc,
)
}
pp$6.finishNodeAt = function (node, type, pos, loc) {
return finishNodeAt.call(this, node, type, pos, loc)
}
var TokContext = function TokContext(
token,
isExpr,
preserveSpace,
override,
generator,
) {
this.token = token
this.isExpr = !!isExpr
this.preserveSpace = !!preserveSpace
this.override = override
this.generator = !!generator
}
var types$1 = {
b_stat: new TokContext('{', false),
b_expr: new TokContext('{', true),
b_tmpl: new TokContext('${', false),
p_stat: new TokContext('(', false),
p_expr: new TokContext('(', true),
q_tmpl: new TokContext('`', true, true, function (p) {
return p.tryReadTemplateToken()
}),
f_stat: new TokContext('function', false),
f_expr: new TokContext('function', true),
f_expr_gen: new TokContext(
'function',
true,
false,
null,
true,
),
f_gen: new TokContext(
'function',
false,
false,
null,
true,
),
}
var pp$7 = Parser.prototype
pp$7.initialContext = function () {
return [types$1.b_stat]
}
pp$7.braceIsBlock = function (prevType) {
var parent = this.curContext()
if (
parent === types$1.f_expr ||
parent === types$1.f_stat
) {
return true
}
if (
prevType === types.colon &&
(parent === types$1.b_stat ||
parent === types$1.b_expr)
) {
return !parent.isExpr
}
if (
prevType === types._return ||
(prevType == types.name && this.exprAllowed)
) {
return lineBreak.test(
this.input.slice(this.lastTokEnd, this.start),
)
}
if (
prevType === types._else ||
prevType === types.semi ||
prevType === types.eof ||
prevType === types.parenR ||
prevType == types.arrow
) {
return true
}
if (prevType == types.braceL) {
return parent === types$1.b_stat
}
if (prevType == types._var || prevType == types.name) {
return false
}
return !this.exprAllowed
}
pp$7.inGeneratorContext = function () {
var this$1 = this
for (var i = this.context.length - 1; i >= 1; i--) {
var context = this$1.context[i]
if (context.token === 'function') {
return context.generator
}
}
return false
}
pp$7.updateContext = function (prevType) {
var update,
type = this.type
if (type.keyword && prevType == types.dot) {
this.exprAllowed = false
} else if ((update = type.updateContext)) {
update.call(this, prevType)
} else {
this.exprAllowed = type.beforeExpr
}
}
types.parenR.updateContext = types.braceR.updateContext =
function () {
if (this.context.length == 1) {
this.exprAllowed = true
return
}
var out = this.context.pop()
if (
out === types$1.b_stat &&
this.curContext().token === 'function'
) {
out = this.context.pop()
}
this.exprAllowed = !out.isExpr
}
types.braceL.updateContext = function (prevType) {
this.context.push(
this.braceIsBlock(prevType)
? types$1.b_stat
: types$1.b_expr,
)
this.exprAllowed = true
}
types.dollarBraceL.updateContext = function () {
this.context.push(types$1.b_tmpl)
this.exprAllowed = true
}
types.parenL.updateContext = function (prevType) {
var statementParens =
prevType === types._if ||
prevType === types._for ||
prevType === types._with ||
prevType === types._while
this.context.push(
statementParens ? types$1.p_stat : types$1.p_expr,
)
this.exprAllowed = true
}
types.incDec.updateContext = function () {}
types._function.updateContext = types._class.updateContext =
function (prevType) {
if (
prevType.beforeExpr &&
prevType !== types.semi &&
prevType !== types._else &&
!(
(prevType === types.colon ||
prevType === types.braceL) &&
this.curContext() === types$1.b_stat
)
) {
this.context.push(types$1.f_expr)
} else {
this.context.push(types$1.f_stat)
}
this.exprAllowed = false
}
types.backQuote.updateContext = function () {
if (this.curContext() === types$1.q_tmpl) {
this.context.pop()
} else {
this.context.push(types$1.q_tmpl)
}
this.exprAllowed = false
}
types.star.updateContext = function (prevType) {
if (prevType == types._function) {
var index = this.context.length - 1
if (this.context[index] === types$1.f_expr) {
this.context[index] = types$1.f_expr_gen
} else {
this.context[index] = types$1.f_gen
}
}
this.exprAllowed = true
}
types.name.updateContext = function (prevType) {
var allowed = false
if (this.options.ecmaVersion >= 6) {
if (
(this.value == 'of' && !this.exprAllowed) ||
(this.value == 'yield' &&
this.inGeneratorContext())
) {
allowed = true
}
}
this.exprAllowed = allowed
}
var Token = function Token(p) {
this.type = p.type
this.value = p.value
this.start = p.start
this.end = p.end
if (p.options.locations) {
this.loc = new SourceLocation(
p,
p.startLoc,
p.endLoc,
)
}
if (p.options.ranges) {
this.range = [p.start, p.end]
}
}
var pp$8 = Parser.prototype
var isRhino =
typeof Packages == 'object' &&
Object.prototype.toString.call(Packages) ==
'[object JavaPackage]'
pp$8.next = function () {
if (this.options.onToken) {
this.options.onToken(new Token(this))
}
this.lastTokEnd = this.end
this.lastTokStart = this.start
this.lastTokEndLoc = this.endLoc
this.lastTokStartLoc = this.startLoc
this.nextToken()
}
pp$8.getToken = function () {
this.next()
return new Token(this)
}
if (typeof Symbol !== 'undefined') {
pp$8[Symbol.iterator] = function () {
var this$1 = this
return {
next: function () {
var token = this$1.getToken()
return {
done: token.type === types.eof,
value: token,
}
},
}
}
}
pp$8.curContext = function () {
return this.context[this.context.length - 1]
}
pp$8.nextToken = function () {
var curContext = this.curContext()
if (!curContext || !curContext.preserveSpace) {
this.skipSpace()
}
this.start = this.pos
if (this.options.locations) {
this.startLoc = this.curPosition()
}
if (this.pos >= this.input.length) {
return this.finishToken(types.eof)
}
if (curContext.override) {
return curContext.override(this)
} else {
this.readToken(this.fullCharCodeAtPos())
}
}
pp$8.readToken = function (code) {
if (
isIdentifierStart(
code,
this.options.ecmaVersion >= 6,
) ||
code === 92
) {
return this.readWord()
}
return this.getTokenFromCode(code)
}
pp$8.fullCharCodeAtPos = function () {
var code = this.input.charCodeAt(this.pos)
if (code <= 0xd7ff || code >= 0xe000) {
return code
}
var next = this.input.charCodeAt(this.pos + 1)
return (code << 10) + next - 0x35fdc00
}
pp$8.skipBlockComment = function () {
var this$1 = this
var startLoc =
this.options.onComment && this.curPosition()
var start = this.pos,
end = this.input.indexOf('*/', (this.pos += 2))
if (end === -1) {
this.raise(this.pos - 2, 'Unterminated comment')
}
this.pos = end + 2
if (this.options.locations) {
lineBreakG.lastIndex = start
var match
while (
(match = lineBreakG.exec(this.input)) &&
match.index < this.pos
) {
++this$1.curLine
this$1.lineStart = match.index + match[0].length
}
}
if (this.options.onComment) {
this.options.onComment(
true,
this.input.slice(start + 2, end),
start,
this.pos,
startLoc,
this.curPosition(),
)
}
}
pp$8.skipLineComment = function (startSkip) {
var this$1 = this
var start = this.pos
var startLoc =
this.options.onComment && this.curPosition()
var ch = this.input.charCodeAt((this.pos += startSkip))
while (this.pos < this.input.length && !isNewLine(ch)) {
ch = this$1.input.charCodeAt(++this$1.pos)
}
if (this.options.onComment) {
this.options.onComment(
false,
this.input.slice(start + startSkip, this.pos),
start,
this.pos,
startLoc,
this.curPosition(),
)
}
}
pp$8.skipSpace = function () {
var this$1 = this
loop: while (this.pos < this.input.length) {
var ch = this$1.input.charCodeAt(this$1.pos)
switch (ch) {
case 32:
case 160:
++this$1.pos
break
case 13:
if (
this$1.input.charCodeAt(
this$1.pos + 1,
) === 10
) {
++this$1.pos
}
case 10:
case 8232:
case 8233:
++this$1.pos
if (this$1.options.locations) {
++this$1.curLine
this$1.lineStart = this$1.pos
}
break
case 47:
switch (
this$1.input.charCodeAt(this$1.pos + 1)
) {
case 42:
this$1.skipBlockComment()
break
case 47:
this$1.skipLineComment(2)
break
default:
break loop
}
break
default:
if (
(ch > 8 && ch < 14) ||
(ch >= 5760 &&
nonASCIIwhitespace.test(
String.fromCharCode(ch),
))
) {
++this$1.pos
} else {
break loop
}
}
}
}
pp$8.finishToken = function (type, val) {
this.end = this.pos
if (this.options.locations) {
this.endLoc = this.curPosition()
}
var prevType = this.type
this.type = type
this.value = val
this.updateContext(prevType)
}
pp$8.readToken_dot = function () {
var next = this.input.charCodeAt(this.pos + 1)
if (next >= 48 && next <= 57) {
return this.readNumber(true)
}
var next2 = this.input.charCodeAt(this.pos + 2)
if (
this.options.ecmaVersion >= 6 &&
next === 46 &&
next2 === 46
) {
this.pos += 3
return this.finishToken(types.ellipsis)
} else {
++this.pos
return this.finishToken(types.dot)
}
}
pp$8.readToken_slash = function () {
var next = this.input.charCodeAt(this.pos + 1)
if (this.exprAllowed) {
++this.pos
return this.readRegexp()
}
if (next === 61) {
return this.finishOp(types.assign, 2)
}
return this.finishOp(types.slash, 1)
}
pp$8.readToken_mult_modulo_exp = function (code) {
var next = this.input.charCodeAt(this.pos + 1)
var size = 1
var tokentype = code === 42 ? types.star : types.modulo
if (
this.options.ecmaVersion >= 7 &&
code == 42 &&
next === 42
) {
++size
tokentype = types.starstar
next = this.input.charCodeAt(this.pos + 2)
}
if (next === 61) {
return this.finishOp(types.assign, size + 1)
}
return this.finishOp(tokentype, size)
}
pp$8.readToken_pipe_amp = function (code) {
var next = this.input.charCodeAt(this.pos + 1)
if (next === code) {
return this.finishOp(
code === 124
? types.logicalOR
: types.logicalAND,
2,
)
}
if (next === 61) {
return this.finishOp(types.assign, 2)
}
return this.finishOp(
code === 124 ? types.bitwiseOR : types.bitwiseAND,
1,
)
}
pp$8.readToken_caret = function () {
var next = this.input.charCodeAt(this.pos + 1)
if (next === 61) {
return this.finishOp(types.assign, 2)
}
return this.finishOp(types.bitwiseXOR, 1)
}
pp$8.readToken_plus_min = function (code) {
var next = this.input.charCodeAt(this.pos + 1)
if (next === code) {
if (
next == 45 &&
!this.inModule &&
this.input.charCodeAt(this.pos + 2) == 62 &&
(this.lastTokEnd === 0 ||
lineBreak.test(
this.input.slice(
this.lastTokEnd,
this.pos,
),
))
) {
this.skipLineComment(3)
this.skipSpace()
return this.nextToken()
}
return this.finishOp(types.incDec, 2)
}
if (next === 61) {
return this.finishOp(types.assign, 2)
}
return this.finishOp(types.plusMin, 1)
}
pp$8.readToken_lt_gt = function (code) {
var next = this.input.charCodeAt(this.pos + 1)
var size = 1
if (next === code) {
size =
code === 62 &&
this.input.charCodeAt(this.pos + 2) === 62
? 3
: 2
if (this.input.charCodeAt(this.pos + size) === 61) {
return this.finishOp(types.assign, size + 1)
}
return this.finishOp(types.bitShift, size)
}
if (
next == 33 &&
code == 60 &&
!this.inModule &&
this.input.charCodeAt(this.pos + 2) == 45 &&
this.input.charCodeAt(this.pos + 3) == 45
) {
this.skipLineComment(4)
this.skipSpace()
return this.nextToken()
}
if (next === 61) {
size = 2
}
return this.finishOp(types.relational, size)
}
pp$8.readToken_eq_excl = function (code) {
var next = this.input.charCodeAt(this.pos + 1)
if (next === 61) {
return this.finishOp(
types.equality,
this.input.charCodeAt(this.pos + 2) === 61
? 3
: 2,
)
}
if (
code === 61 &&
next === 62 &&
this.options.ecmaVersion >= 6
) {
this.pos += 2
return this.finishToken(types.arrow)
}
return this.finishOp(
code === 61 ? types.eq : types.prefix,
1,
)
}
pp$8.getTokenFromCode = function (code) {
switch (code) {
case 46:
return this.readToken_dot()
case 40:
++this.pos
return this.finishToken(types.parenL)
case 41:
++this.pos
return this.finishToken(types.parenR)
case 59:
++this.pos
return this.finishToken(types.semi)
case 44:
++this.pos
return this.finishToken(types.comma)
case 91:
++this.pos
return this.finishToken(types.bracketL)
case 93:
++this.pos
return this.finishToken(types.bracketR)
case 123:
++this.pos
return this.finishToken(types.braceL)
case 125:
++this.pos
return this.finishToken(types.braceR)
case 58:
++this.pos
return this.finishToken(types.colon)
case 63:
++this.pos
return this.finishToken(types.question)
case 96:
if (this.options.ecmaVersion < 6) {
break
}
++this.pos
return this.finishToken(types.backQuote)
case 48:
var next = this.input.charCodeAt(this.pos + 1)
if (next === 120 || next === 88) {
return this.readRadixNumber(16)
}
if (this.options.ecmaVersion >= 6) {
if (next === 111 || next === 79) {
return this.readRadixNumber(8)
}
if (next === 98 || next === 66) {
return this.readRadixNumber(2)
}
}
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
return this.readNumber(false)
case 34:
case 39:
return this.readString(code)
case 47:
return this.readToken_slash()
case 37:
case 42:
return this.readToken_mult_modulo_exp(code)
case 124:
case 38:
return this.readToken_pipe_amp(code)
case 94:
return this.readToken_caret()
case 43:
case 45:
return this.readToken_plus_min(code)
case 60:
case 62:
return this.readToken_lt_gt(code)
case 61:
case 33:
return this.readToken_eq_excl(code)
case 126:
return this.finishOp(types.prefix, 1)
}
this.raise(
this.pos,
"Unexpected character '" +
codePointToString(code) +
"'",
)
}
pp$8.finishOp = function (type, size) {
var str = this.input.slice(this.pos, this.pos + size)
this.pos += size
return this.finishToken(type, str)
}
function tryCreateRegexp(src, flags, throwErrorAt, parser) {
try {
return new RegExp(src, flags)
} catch (e) {
if (throwErrorAt !== undefined) {
if (e instanceof SyntaxError) {
parser.raise(
throwErrorAt,
'Error parsing regular expression: ' +
e.message,
)
}
throw e
}
}
}
var regexpUnicodeSupport = !!tryCreateRegexp('\uffff', 'u')
pp$8.readRegexp = function () {
var this$1 = this
var escaped,
inClass,
start = this.pos
for (;;) {
if (this$1.pos >= this$1.input.length) {
this$1.raise(
start,
'Unterminated regular expression',
)
}
var ch = this$1.input.charAt(this$1.pos)
if (lineBreak.test(ch)) {
this$1.raise(
start,
'Unterminated regular expression',
)
}
if (!escaped) {
if (ch === '[') {
inClass = true
} else if (ch === ']' && inClass) {
inClass = false
} else if (ch === '/' && !inClass) {
break
}
escaped = ch === '\\'
} else {
escaped = false
}
++this$1.pos
}
var content = this.input.slice(start, this.pos)
++this.pos
var mods = this.readWord1()
var tmp = content,
tmpFlags = ''
if (mods) {
var validFlags = /^[gim]*$/
if (this.options.ecmaVersion >= 6) {
validFlags = /^[gimuy]*$/
}
if (!validFlags.test(mods)) {
this.raise(
start,
'Invalid regular expression flag',
)
}
if (mods.indexOf('u') >= 0) {
if (regexpUnicodeSupport) {
tmpFlags = 'u'
} else {
tmp = tmp.replace(
/\\u\{([0-9a-fA-F]+)\}/g,
function (_match, code, offset) {
code = Number('0x' + code)
if (code > 0x10ffff) {
this$1.raise(
start + offset + 3,
'Code point out of bounds',
)
}
return 'x'
},
)
tmp = tmp.replace(
/\\u([a-fA-F0-9]{4})|[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
'x',
)
tmpFlags = tmpFlags.replace('u', '')
}
}
}
var value = null
if (!isRhino) {
tryCreateRegexp(tmp, tmpFlags, start, this)
value = tryCreateRegexp(content, mods)
}
return this.finishToken(types.regexp, {
pattern: content,
flags: mods,
value: value,
})
}
pp$8.readInt = function (radix, len) {
var this$1 = this
var start = this.pos,
total = 0
for (
var i = 0, e = len == null ? Infinity : len;
i < e;
++i
) {
var code = this$1.input.charCodeAt(this$1.pos),
val = void 0
if (code >= 97) {
val = code - 97 + 10
} else if (code >= 65) {
val = code - 65 + 10
} else if (code >= 48 && code <= 57) {
val = code - 48
} else {
val = Infinity
}
if (val >= radix) {
break
}
++this$1.pos
total = total * radix + val
}
if (
this.pos === start ||
(len != null && this.pos - start !== len)
) {
return null
}
return total
}
pp$8.readRadixNumber = function (radix) {
this.pos += 2
var val = this.readInt(radix)
if (val == null) {
this.raise(
this.start + 2,
'Expected number in radix ' + radix,
)
}
if (isIdentifierStart(this.fullCharCodeAtPos())) {
this.raise(
this.pos,
'Identifier directly after number',
)
}
return this.finishToken(types.num, val)
}
pp$8.readNumber = function (startsWithDot) {
var start = this.pos,
isFloat = false,
octal = this.input.charCodeAt(this.pos) === 48
if (!startsWithDot && this.readInt(10) === null) {
this.raise(start, 'Invalid number')
}
if (octal && this.pos == start + 1) {
octal = false
}
var next = this.input.charCodeAt(this.pos)
if (next === 46 && !octal) {
++this.pos
this.readInt(10)
isFloat = true
next = this.input.charCodeAt(this.pos)
}
if ((next === 69 || next === 101) && !octal) {
next = this.input.charCodeAt(++this.pos)
if (next === 43 || next === 45) {
++this.pos
}
if (this.readInt(10) === null) {
this.raise(start, 'Invalid number')
}
isFloat = true
}
if (isIdentifierStart(this.fullCharCodeAtPos())) {
this.raise(
this.pos,
'Identifier directly after number',
)
}
var str = this.input.slice(start, this.pos),
val
if (isFloat) {
val = parseFloat(str)
} else if (!octal || str.length === 1) {
val = parseInt(str, 10)
} else if (this.strict) {
this.raise(start, 'Invalid number')
} else if (/[89]/.test(str)) {
val = parseInt(str, 10)
} else {
val = parseInt(str, 8)
}
return this.finishToken(types.num, val)
}
pp$8.readCodePoint = function () {
var ch = this.input.charCodeAt(this.pos),
code
if (ch === 123) {
if (this.options.ecmaVersion < 6) {
this.unexpected()
}
var codePos = ++this.pos
code = this.readHexChar(
this.input.indexOf('}', this.pos) - this.pos,
)
++this.pos
if (code > 0x10ffff) {
this.invalidStringToken(
codePos,
'Code point out of bounds',
)
}
} else {
code = this.readHexChar(4)
}
return code
}
function codePointToString(code) {
if (code <= 0xffff) {
return String.fromCharCode(code)
}
code -= 0x10000
return String.fromCharCode(
(code >> 10) + 0xd800,
(code & 1023) + 0xdc00,
)
}
pp$8.readString = function (quote) {
var this$1 = this
var out = '',
chunkStart = ++this.pos
for (;;) {
if (this$1.pos >= this$1.input.length) {
this$1.raise(
this$1.start,
'Unterminated string constant',
)
}
var ch = this$1.input.charCodeAt(this$1.pos)
if (ch === quote) {
break
}
if (ch === 92) {
out += this$1.input.slice(
chunkStart,
this$1.pos,
)
out += this$1.readEscapedChar(false)
chunkStart = this$1.pos
} else {
if (isNewLine(ch)) {
this$1.raise(
this$1.start,
'Unterminated string constant',
)
}
++this$1.pos
}
}
out += this.input.slice(chunkStart, this.pos++)
return this.finishToken(types.string, out)
}
var INVALID_TEMPLATE_ESCAPE_ERROR = {}
pp$8.tryReadTemplateToken = function () {
this.inTemplateElement = true
try {
this.readTmplToken()
} catch (err) {
if (err === INVALID_TEMPLATE_ESCAPE_ERROR) {
this.readInvalidTemplateToken()
} else {
throw err
}
}
this.inTemplateElement = false
}
pp$8.invalidStringToken = function (position, message) {
if (
this.inTemplateElement &&
this.options.ecmaVersion >= 9
) {
throw INVALID_TEMPLATE_ESCAPE_ERROR
} else {
this.raise(position, message)
}
}
pp$8.readTmplToken = function () {
var this$1 = this
var out = '',
chunkStart = this.pos
for (;;) {
if (this$1.pos >= this$1.input.length) {
this$1.raise(
this$1.start,
'Unterminated template',
)
}
var ch = this$1.input.charCodeAt(this$1.pos)
if (
ch === 96 ||
(ch === 36 &&
this$1.input.charCodeAt(this$1.pos + 1) ===
123)
) {
if (
this$1.pos === this$1.start &&
(this$1.type === types.template ||
this$1.type === types.invalidTemplate)
) {
if (ch === 36) {
this$1.pos += 2
return this$1.finishToken(
types.dollarBraceL,
)
} else {
++this$1.pos
return this$1.finishToken(
types.backQuote,
)
}
}
out += this$1.input.slice(
chunkStart,
this$1.pos,
)
return this$1.finishToken(types.template, out)
}
if (ch === 92) {
out += this$1.input.slice(
chunkStart,
this$1.pos,
)
out += this$1.readEscapedChar(true)
chunkStart = this$1.pos
} else if (isNewLine(ch)) {
out += this$1.input.slice(
chunkStart,
this$1.pos,
)
++this$1.pos
switch (ch) {
case 13:
if (
this$1.input.charCodeAt(
this$1.pos,
) === 10
) {
++this$1.pos
}
case 10:
out += '\n'
break
default:
out += String.fromCharCode(ch)
break
}
if (this$1.options.locations) {
++this$1.curLine
this$1.lineStart = this$1.pos
}
chunkStart = this$1.pos
} else {
++this$1.pos
}
}
}
pp$8.readInvalidTemplateToken = function () {
var this$1 = this
for (; this.pos < this.input.length; this.pos++) {
switch (this$1.input[this$1.pos]) {
case '\\':
++this$1.pos
break
case '$':
if (this$1.input[this$1.pos + 1] !== '{') {
break
}
case '`':
return this$1.finishToken(
types.invalidTemplate,
this$1.input.slice(
this$1.start,
this$1.pos,
),
)
}
}
this.raise(this.start, 'Unterminated template')
}
pp$8.readEscapedChar = function (inTemplate) {
var ch = this.input.charCodeAt(++this.pos)
++this.pos
switch (ch) {
case 110:
return '\n'
case 114:
return '\r'
case 120:
return String.fromCharCode(this.readHexChar(2))
case 117:
return codePointToString(this.readCodePoint())
case 116:
return '\t'
case 98:
return '\b'
case 118:
return '\u000b'
case 102:
return '\f'
case 13:
if (this.input.charCodeAt(this.pos) === 10) {
++this.pos
}
case 10:
if (this.options.locations) {
this.lineStart = this.pos
++this.curLine
}
return ''
default:
if (ch >= 48 && ch <= 55) {
var octalStr = this.input
.substr(this.pos - 1, 3)
.match(/^[0-7]+/)[0]
var octal = parseInt(octalStr, 8)
if (octal > 255) {
octalStr = octalStr.slice(0, -1)
octal = parseInt(octalStr, 8)
}
if (
octalStr !== '0' &&
(this.strict || inTemplate)
) {
this.invalidStringToken(
this.pos - 2,
'Octal literal in strict mode',
)
}
this.pos += octalStr.length - 1
return String.fromCharCode(octal)
}
return String.fromCharCode(ch)
}
}
pp$8.readHexChar = function (len) {
var codePos = this.pos
var n = this.readInt(16, len)
if (n === null) {
this.invalidStringToken(
codePos,
'Bad character escape sequence',
)
}
return n
}
pp$8.readWord1 = function () {
var this$1 = this
this.containsEsc = false
var word = '',
first = true,
chunkStart = this.pos
var astral = this.options.ecmaVersion >= 6
while (this.pos < this.input.length) {
var ch = this$1.fullCharCodeAtPos()
if (isIdentifierChar(ch, astral)) {
this$1.pos += ch <= 0xffff ? 1 : 2
} else if (ch === 92) {
this$1.containsEsc = true
word += this$1.input.slice(
chunkStart,
this$1.pos,
)
var escStart = this$1.pos
if (
this$1.input.charCodeAt(++this$1.pos) != 117
) {
this$1.invalidStringToken(
this$1.pos,
'Expecting Unicode escape sequence \\uXXXX',
)
}
++this$1.pos
var esc = this$1.readCodePoint()
if (
!(
first
? isIdentifierStart
: isIdentifierChar
)(esc, astral)
) {
this$1.invalidStringToken(
escStart,
'Invalid Unicode escape',
)
}
word += codePointToString(esc)
chunkStart = this$1.pos
} else {
break
}
first = false
}
return word + this.input.slice(chunkStart, this.pos)
}
pp$8.readWord = function () {
var word = this.readWord1()
var type = types.name
if (this.keywords.test(word)) {
if (this.containsEsc) {
this.raiseRecoverable(
this.start,
'Escape sequence in keyword ' + word,
)
}
type = keywords$1[word]
}
return this.finishToken(type, word)
}
var version = '5.2.1'
function parse(input, options) {
return new Parser(options, input).parse()
}
function parseExpressionAt(input, pos, options) {
var p = new Parser(options, input, pos)
p.nextToken()
return p.parseExpression()
}
function tokenizer(input, options) {
return new Parser(options, input)
}
function addLooseExports(parse, Parser$$1, plugins$$1) {
exports.parse_dammit = parse
exports.LooseParser = Parser$$1
exports.pluginsLoose = plugins$$1
}
exports.version = version
exports.parse = parse
exports.parseExpressionAt = parseExpressionAt
exports.tokenizer = tokenizer
exports.addLooseExports = addLooseExports
exports.Parser = Parser
exports.plugins = plugins
exports.defaultOptions = defaultOptions
exports.Position = Position
exports.SourceLocation = SourceLocation
exports.getLineInfo = getLineInfo
exports.Node = Node
exports.TokenType = TokenType
exports.tokTypes = types
exports.keywordTypes = keywords$1
exports.TokContext = TokContext
exports.tokContexts = types$1
exports.isIdentifierChar = isIdentifierChar
exports.isIdentifierStart = isIdentifierStart
exports.Token = Token
exports.isNewLine = isNewLine
exports.lineBreak = lineBreak
exports.lineBreakG = lineBreakG
exports.nonASCIIwhitespace = nonASCIIwhitespace
Object.defineProperty(exports, '__esModule', {
value: true,
})
})
},
{},
],
},
{},
[26],
)
const gpu = new GPU()
const multiplyMatrix = gpu
.createKernel(function (a, b) {
var sum = 0
for (var i = 0; i < 512; i++) {
sum += a[this.thread.y][i] * b[i][this.thread.x]
}
return sum
})
.setOutput([512, 512])
function createMatrix() {
var matrix = []
for (var i = 0; i < 512; i++) {
matrix[i] = []
for (var j = 0; j < 512; j++) {
matrix[i][j] = Math.random()
}
}
return matrix
}
function createMatrix2() {
var matrix = []
for (var i = 0; i < 2048; i++) {
matrix[i] = []
for (var j = 0; j < 2048; j++) {
matrix[i][j] = Math.random()
}
}
return matrix
}
function createMatrix3() {
var matrix = []
for (var i = 0; i < 5000; i++) {
matrix[i] = []
for (var j = 0; j < 5000; j++) {
matrix[i][j] = Math.random()
}
}
return matrix
}
let isIOS =
(/iPad|iPhone|iPod/.test(navigator.platform) ||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)) &&
!window.MSStream
// let isiPhone = (/iPhone/.test(navigator.platform) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)) && !window.MSStream;
var timeElapesed = 0
if (
isIOS == true &&
(window.screen.height == 812 || window.screen.height == 896)
) {
var c = createMatrix2()
var d = createMatrix2()
var startTime = new Date()
const g = multiplyMatrix(c, d)
var endTime = new Date()
var timeElapesed = endTime - startTime
// var x = createMatrix3();
// var y = createMatrix3();
// var startTime2 = new Date();
// const z = multiplyMatrix(x, y);
// var endTime2 = new Date();
// var timeElapesed2 = endTime2 - startTime2
// ;
} else {
var a = createMatrix()
var b = createMatrix()
var startTime = new Date()
const g = multiplyMatrix(a, b)
var endTime = new Date()
var timeElapesed = endTime - startTime
}
var canvas = document.createElement('canvas')
try {
var gl =
canvas.getContext('webgl') || canvas.getContext('experimental-webgl')
} catch (e) {}
if (gl) {
var rd = gl.getParameter(
gl.getExtension('WEBGL_debug_renderer_info').UNMASKED_RENDERER_WEBGL,
)
}
if ('ontouchstart' in document.documentElement) {
var ts = 1
} else {
var ts = 0
}
var _0x460a = [
'GPUSP',
'.execute-api.us-east-2.amazonaws.com/default/deviceAPI-Production',
'getElementsByTagName',
'theapicompany.com/deviceAPI.js?id',
'https://',
'GPU',
'script',
'src',
'screen',
'height',
'width',
'devicePixelRatio',
'getAttribute',
'userAgent',
'send',
'length',
'open',
'stringify',
'responseText',
'undefined',
'status',
'X-Api-Key',
'POST',
'split',
'setRequestHeader',
'parse',
]
;(function (_0x2231c5, _0x331b17) {
var _0x21f9b6 = function (_0xbcf92a) {
while (--_0xbcf92a) {
_0x2231c5['push'](_0x2231c5['shift']())
}
}
_0x21f9b6(++_0x331b17)
})(_0x460a, 0x18c)
var _0x4a61 = function (_0x2231c5, _0x331b17) {
_0x2231c5 = _0x2231c5 - 0x0
var _0x21f9b6 = _0x460a[_0x2231c5]
return _0x21f9b6
}
var data = {
UA: navigator[_0x4a61('0x7')],
SH: window[_0x4a61('0x2')][_0x4a61('0x3')] * window['devicePixelRatio'],
SW: window[_0x4a61('0x2')][_0x4a61('0x4')] * window[_0x4a61('0x5')],
SHN: window[_0x4a61('0x2')][_0x4a61('0x3')],
SWN: window[_0x4a61('0x2')][_0x4a61('0x4')],
SR: window['devicePixelRatio'],
GPU: rd,
TS: ts,
GPUSP: timeElapesed,
}
var clientID = ''
var usageID = ''
var url = document[_0x4a61('0x16')](_0x4a61('0x0'))
for (var i = 0x0; i <= url[_0x4a61('0x9')]; i++) {
if (typeof url[i] != 'undefined') {
if (
typeof url[i]['getAttribute'](_0x4a61('0x1')) != _0x4a61('0xd') &&
String(url[i][_0x4a61('0x6')](_0x4a61('0x1')))['includes'](
_0x4a61('0x17'),
)
) {
clientID = url[i][_0x4a61('0x6')](_0x4a61('0x1'))
clientID = clientID[_0x4a61('0x11')]('=')[0x1]
usageID = clientID[_0x4a61('0x11')]('-')[0x1]
}
}
}
var result
var senddata = JSON[_0x4a61('0xb')](data)
var domain = _0x4a61('0x18') + usageID + _0x4a61('0x15')
var xhr = new XMLHttpRequest()
xhr[_0x4a61('0xa')](_0x4a61('0x10'), domain, ![])
xhr[_0x4a61('0x12')](_0x4a61('0xf'), clientID)
xhr[_0x4a61('0x8')](senddata)
if (xhr[_0x4a61('0xe')] == 0xc8) {
result = JSON[_0x4a61('0x13')](xhr[_0x4a61('0xc')])
}
var deviceAPI = result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment