Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const { Syntax } = require('estraverse');
const { getParentNode, getCurrentKey } = require('../controller-utils');
const caputuringTargetTypes = [
// Syntax.Property,
Syntax.ObjectExpression,
Syntax.ArrayExpression,
// Syntax.ConditionalExpression,
Syntax.Identifier,
Syntax.MemberExpression,
Syntax.CallExpression,
Syntax.UnaryExpression,
Syntax.BinaryExpression,
Syntax.LogicalExpression,
Syntax.AssignmentExpression,
Syntax.NewExpression,
Syntax.UpdateExpression,
Syntax.YieldExpression,
Syntax.AwaitExpression,
Syntax.TemplateLiteral,
Syntax.TaggedTemplateExpression
];
const isCaputuringTargetType = (currentNode) => {
return caputuringTargetTypes.indexOf(currentNode.type) !== -1;
};
const isCalleeOfParent = (parentNode, currentKey) => {
return (parentNode.type === Syntax.CallExpression || parentNode.type === Syntax.NewExpression) && currentKey === 'callee';
};
const isChildOfTaggedTemplateExpression = (parentNode) => {
function getStaticValue(node) {
if (node.type === Syntax.Literal) {
var value = node.raw !== undefined ? node.raw : node.value;
var number = parseFloat(value);
if (!isNaN(number))
return number;
value = node.value;
switch(value) {
case "true": return true;
case "false": return false;
case "null": return null;
default: return value;
}
}
if (node.type == Syntax.MemberExpression || node.type == Syntax.CallExpression || node.type == Syntax.Identifier || node.type == Syntax.NewExpression || node.type == Syntax.LogicalExpression) {
return ANNO(node).getStaticValue();
}
if (node.type === Syntax.UnaryExpression) {
if (node.operator == "typeof") {
return ANNO(node).getStaticValue();
}
if(UnaryFunctions.hasOwnProperty(node.operator)) {
return UnaryFunctions[node.operator](getStaticValue(node.argument));
}
Shade.throwError(node, "Unknown unary operator: " + node.operator);
}
if (node.type === Syntax.BinaryExpression) {
if(BinaryFunctions.hasOwnProperty(node.operator)) {
return BinaryFunctions[node.operator](getStaticValue(node.left), getStaticValue(node.right));
}
Shade.throwError(node, "Unknown binary operator: " + node.operator);
function createOSLClosure(node, closureName, color) {
var callee = node.callee;
assert(callee.type == Syntax.MemberExpression);
var closure = color ? createScaledClosure(color, closureName, node) : createSimpleClosure(closureName, node);
if (callee.object.type == Syntax.NewExpression)
return closure;
//assert.equal(callee.object.type, Syntax.BinaryExpression);
return {
type: Syntax.BinaryExpression,
operator: "+",
left: callee.object,
right: closure,
extra: {
type: Shade.TYPES.OBJECT,
kind: Shade.OBJECT_KINDS.COLOR_CLOSURE
}
}
}
const isCalleeOfParent = (parentNode, currentKey) => {
return (parentNode.type === Syntax.CallExpression || parentNode.type === Syntax.NewExpression) && currentKey === 'callee';
};
property: function (node, parent, context, state) {
var parameterName = Tools.getNameForSystem(SystemDefines.CANVAS_DIMENSIONS);
state.usedParameters.system[parameterName] = state.systemParameters[SystemDefines.CANVAS_DIMENSIONS];
return {
type: Syntax.NewExpression,
callee: {
type: Syntax.Identifier,
name: "Vec3"
},
arguments: [
{
type: Syntax.BinaryExpression,
left: {
type: Syntax.MemberExpression,
object: {
type: Syntax.Identifier,
name: "gl_FragCoord"
},
property: {
type: Syntax.Identifier,
name: "xyz"
generateVecFromArgs: function(vecCount, args){
if(vecCount == 1)
return args[0];
if(args.length == 0){
args = Vec.getVecArgs(args);
}
if(args.length == 1 && ANNO(args[0]).isOfKind(KINDS['FLOAT' + vecCount]))
return args[0];
var result = {
type: Syntax.NewExpression,
callee: {
type: Syntax.Identifier,
name: "Vec" + vecCount
},
arguments: args
};
ANNO(result).setType(TYPES.OBJECT, KINDS['FLOAT' + vecCount]);
ANNO(result.callee).setType(TYPES.FUNCTION);
return result;
},
property: function (node, parent, context, state) {
var parameterName = Tools.getNameForSystem(SystemDefines.CANVAS_DIMENSIONS);
var canvasDimensions = state.systemParameters[SystemDefines.CANVAS_DIMENSIONS];
if(!canvasDimensions)
Shade.throwError(node, "Internal Error: No canavas dimensions defined" );
state.usedParameters.system[parameterName] = canvasDimensions;
return {
type: Syntax.NewExpression,
callee: {
type: Syntax.Identifier,
name: "Vec3"
},
arguments: [
{
type: Syntax.BinaryExpression,
left: {
type: Syntax.MemberExpression,
object: {
type: Syntax.Identifier,
name: "gl_FragCoord"
},
property: {
type: Syntax.Identifier,
name: "xyz"
generateMatFromArgs: function (matName, args) {
if (args.length == 0) {
args = Vec.getVecArgs(args);
}
if (args.length == 1 && ANNO(args[0]).isOfKind(Mat.TYPES[matName].kind))
return args[0];
var result = {
type: Syntax.NewExpression,
callee: {
type: Syntax.Identifier,
name: matName
},
arguments: args
};
ANNO(result).setType(TYPES.OBJECT, Mat.TYPES[matName].kind);
ANNO(result.callee).setType(TYPES.FUNCTION);
return result;
},