Skip to content

Commit 33a760c

Browse files
Xiaoji Chendy
authored andcommitted
add comments; code style
1 parent 8c6a843 commit 33a760c

File tree

2 files changed

+882
-12
lines changed

2 files changed

+882
-12
lines changed

lib/index.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ GLSL.prototype.transforms = {
404404
var inArgs = []
405405
var outArgs = []
406406
args.components.forEach(function (arg, index) {
407-
arg.index = index;
407+
arg.index = index
408408
if (arg.qualifier.slice(0, 2) === 'in') {
409409
inArgs.push(arg)
410410
}
@@ -413,7 +413,7 @@ GLSL.prototype.transforms = {
413413
}
414414
})
415415
if (outArgs.length === 0) {
416-
outArgs = null;
416+
outArgs = null
417417
}
418418

419419
//if main name is registered - provide type-scoped name of function
@@ -427,20 +427,24 @@ GLSL.prototype.transforms = {
427427
//create function body
428428
result += `function ${name} (${args}) {\n`
429429

430-
//input parameters are copied
430+
//guard input parameters from being mutated
431431
inArgs.forEach(function (arg) {
432432
if (/^(vec|mat)/.test(arg.type)) {
433433
result += `${arg} = ${arg}.slice();\n`
434434
}
435435
})
436436

437-
var scope = this.scopes[this.currentScope];
438-
scope.callName = name;
439-
scope.outArgs = outArgs;
437+
//populate current scope information
438+
//this is used by the `return` transform
439+
var scope = this.scopes[this.currentScope]
440+
scope.callName = name
441+
scope.outArgs = outArgs
440442

441443
result += this.process(node.children[2])
442444

443445
if (outArgs && outType === 'void') {
446+
//the output list is usually created when transforming the `return` statement
447+
//but this function does not have a `return` at the end
444448
result += `\n${name}.__out__ = [${outArgs.join(', ')}];`
445449
}
446450

@@ -456,7 +460,9 @@ GLSL.prototype.transforms = {
456460
complexity: 999
457461
})
458462

459-
result.outArgs = outArgs;
463+
//save the output arguments list
464+
//this is used by the `call` transform
465+
result.outArgs = outArgs
460466

461467
//register function descriptor
462468
this.functions[name] = result
@@ -516,6 +522,7 @@ GLSL.prototype.transforms = {
516522
if (node.parent.type === 'functionargs') {
517523
result = this.process(decllist)
518524

525+
// in/out/inout
519526
var qualifier = node.token.data
520527
if (qualifier === result.type) {
521528
result.qualifier = 'in'
@@ -766,18 +773,24 @@ GLSL.prototype.transforms = {
766773

767774
return: function (node) {
768775
var expr = this.process(node.children[0])
769-
var result;
770-
var scope = this.scopes[this.currentScope];
776+
var result
777+
var scope = this.scopes[this.currentScope]
771778
if (scope.outArgs) {
772779
var outStmt = `${scope.callName}.__out__ = [${scope.outArgs.join(', ')}]`
773780

774781
if (expr.visible) {
782+
// func.__return__ = <expression>;
783+
// func.__out__ = [outArg1, outArg2, ...];
784+
// return func.__return__;
775785
result = `${scope.callName}.__return__ = ${expr};\n${outStmt};\nreturn ${scope.callName}.__return__`
776786
} else {
787+
// func.__out__ = [outArg1, outArg2, ...];
788+
// return;
777789
result = `${outStmt};\nreturn`
778790
}
779791
} else {
780-
result = 'return' + (expr.visible ? ' ' + expr : '');
792+
// return <expression>;
793+
result = 'return' + (expr.visible ? ' ' + expr : '')
781794
}
782795
return Descriptor(result, {type: expr.type})
783796
},
@@ -1165,11 +1178,13 @@ GLSL.prototype.transforms = {
11651178
optimize = false
11661179
}
11671180

1168-
var res = `${callName}(${argValues.join(', ')})`;
1181+
var res = `${callName}(${argValues.join(', ')})`
11691182
if (outArgs) {
1183+
// calling func(in a, out b, out c):
1184+
// (func(a, b, c), [b, c] = func.__out__, func.__return__)
11701185
var outList = outArgs.map(function (arg) {
11711186
return argValues[arg.index]
1172-
});
1187+
})
11731188
res = `(${res}, [${outList.join(', ')}] = ${callName}.__out__, ${callName}.__return__)`
11741189
}
11751190

0 commit comments

Comments
 (0)