Skip to content

Commit efe5c99

Browse files
committed
fix(glsl): multiple fixes
1 parent 0d206e1 commit efe5c99

File tree

8 files changed

+84
-30
lines changed

8 files changed

+84
-30
lines changed

src/glsl/builtin.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,4 +354,24 @@ export class BuiltIn {
354354
fun(func) {
355355
return fun(func);
356356
}
357+
358+
equal(x, y) {
359+
if (x === y) {
360+
return true;
361+
}
362+
if (!x && !y) {
363+
return true;
364+
}
365+
if (!x) {
366+
return false;
367+
}
368+
if (x.equals) {
369+
return x.equals(y);
370+
}
371+
return false;
372+
}
373+
374+
notEqual(x, y) {
375+
return !this.equal(x, y);
376+
}
357377
}

src/glsl/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ function handleNode(node) {
8383
return updExp(node);
8484
}
8585
if (type === 'BreakStatement') {
86-
return breSta(node);
86+
return breSta();
8787
}
8888
if (type === 'ContinueStatement') {
89-
return conSta(node);
89+
return conSta();
9090
}
9191
if (type === 'ThrowStatement') {
9292
return thrSta(node);
@@ -352,7 +352,7 @@ function handleBody(body, tabCount = 0) {
352352
.join('\n');
353353
}
354354

355-
export function buildGLSL(fun, { glsl = true, js = false, ast = false } = {}) {
355+
export function buildGLSL(fun, { glsl = true, js = undefined, ast = undefined } = {}) {
356356
// console.log('fun', fun.toString());
357357

358358
let str;

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export function fun(type, func) {
5656
}
5757

5858
export function cls(classDesc) {
59+
if (!(typeof classDesc === 'object')) {
60+
return classDesc;
61+
}
5962
const { constructor, ...proto } = classDesc;
6063

6164
const types = {};

src/jstree.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ function extractType(node, target, options) {
8787
} else if (type === 'ArrowFunctionExpression') {
8888
target.newInit = handleParams(node, options);
8989
target.newInit.returnType = 'void';
90+
} else {
91+
target.newInit = node;
9092
}
9193
return target;
9294
}

test/glsl/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
// @ts-nocheck
12
import { assert } from 'chai';
2-
import { buildGLSL, sampler2D, joinGLSL } from '../../src/glsl';
3+
import { buildGLSL, joinGLSL } from '../../src/glsl';
34

45
describe('glsl tests', () => {
56
it('glsl hello world works.', () => {
@@ -65,7 +66,7 @@ float baz() {
6566
let bar = vec2((x = vec2(1.0, 2.0)) => {
6667
x = normalize(x);
6768
return vec2(x.x, y);
68-
})
69+
});
6970
});
7071
});
7172
});

test/glsl/sim.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ describe('glsl tests', () => {
1010
return MOCKED;
1111
}
1212

13-
function multiplySim(a, b) {
14-
return a.x * b.x;
15-
}
16-
1713
class Vec2 {
1814
constructor(x = 0, y = x) {
1915
this.x = x;
@@ -48,11 +44,10 @@ describe('glsl tests', () => {
4844
}
4945

5046
const jsOptions = {
51-
multiply: multiplySim,
5247
Vec2, Vec3, Vec4,
5348
calc: calcSim
5449
};
55-
buildGLSL(() => { return {} }, { glsl: false, js: jsOptions });
50+
buildGLSL(() => { return {}; }, { glsl: false, js: jsOptions });
5651

5752
it('works glsl vectors.', () => {
5853
const shader = ({ vec2 }) => {
@@ -336,7 +331,7 @@ vec4 bar(vec2 x) {
336331
return foo;
337332
});
338333
let bar = Foo((x = float()) => {
339-
let foo = new Foo(boom(x))
334+
let foo = new Foo(boom(x));
340335
return foo;
341336
});
342337
return { bar };

test/index.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { assert } from 'chai';
22
import { typ, fun, cls, string, number } from '../src';
33

4+
class Test {
5+
get foo() {
6+
return 'foo';
7+
}
8+
}
9+
410
describe('type tests', () => {
511

612

@@ -9,10 +15,6 @@ describe('type tests', () => {
915
// }
1016
// type bla = Parameters<(typeof f1)>;
1117

12-
class Test {
13-
14-
}
15-
1618
it('should work with valid number declarations', () => {
1719
let nr1 = typ(number);
1820

@@ -40,6 +42,7 @@ describe('type tests', () => {
4042
});
4143

4244
it('should work with valid type declarations', () => {
45+
4346
let h1 = typ(Test);
4447

4548
assert.equal(h1, undefined);
@@ -57,19 +60,18 @@ describe('type tests', () => {
5760

5861
describe('function tests', () => {
5962

60-
class Test {
6163

62-
}
6364

6465
it('should work with valid function declarations', () => {
6566
const fn1 = fun(string, (p = typ(number)) => {
66-
return `hallo ${p}`
67+
return `hallo ${p}`;
6768
});
6869

6970
const res1 = fn1(5);
7071
assert.equal(res1, 'hallo 5');
7172

7273
assert.throws(() => {
74+
// @ts-ignore
7375
fn1('string instead of number');
7476
});
7577
});
@@ -88,13 +90,16 @@ describe('function tests', () => {
8890

8991
it('should not work with invalid function declarations', () => {
9092
assert.throws(() => {
93+
// @ts-ignore
9194
const fn = fun((p = typ(number)) => {
92-
return 'hello'
95+
return 'hello';
9396
});
97+
// @ts-ignore
9498
const bla = fn(5);
9599
});
96100

97101
assert.throws(() => {
102+
// @ts-ignore
98103
fun(string, () => {
99104
return 5;
100105
})();
@@ -113,14 +118,14 @@ describe('class tests', () => {
113118
let Test = cls({
114119
bar: number,
115120

116-
constructor: fun(function () {
121+
constructor() {
117122
this.bar = 7;
118-
}),
123+
},
119124

120125
foo: fun(number, function (str = typ(string)) {
121126
return parseFloat(str);
122127
})
123-
})
128+
});
124129

125130
const test = new Test();
126131
const nr = test.foo('12');
@@ -129,32 +134,40 @@ describe('class tests', () => {
129134
assert.equal(nr, 12);
130135

131136
assert.throws(() => {
137+
// @ts-ignore
132138
test.foo(12);
133139
});
134140
});
135141

136142
it('should not work with invalid class declarations', () => {
137143

138144
assert.throws(() => {
139-
let Test = cls({
140-
bar: number,
145+
const Test = cls({
146+
bar: typ(number),
141147

142-
constructor: fun(function () {
143-
this.bar = '12';
148+
constructor() {
149+
this.bar = 12;
150+
},
151+
152+
foo: fun((blub = typ(number)) => {
153+
// FIXME:
154+
this.bar = `${blub}`;
144155
})
145-
})
156+
});
146157

147158
const test = new Test();
159+
160+
test.foo(12);
148161
});
149162
});
150163

151164
it('should work with valid class as type usage', () => {
152165
let Test = cls({
153166
bar: number,
154167

155-
constructor: fun(function () {
168+
constructor() {
156169
this.bar = 7;
157-
}),
170+
},
158171

159172
foo: fun(number, function (str = typ(string)) {
160173
return parseFloat(str);

test/jstree.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,26 @@ describe('jstree implicit tests', () => {
268268
assert.equal(args[1].value, 2);
269269
});
270270

271+
it('extract init from implicit VariableDeclarator inside fun without getting the type', () => {
272+
const node = parse(`let x = Vec2(() => {
273+
let foo = x.yx;
274+
return foo;
275+
});`);
276+
277+
const [declarator] = node.body[0].declarations[0].init.body.body[0].declarations;
278+
const { id, init: { type, object, property } } = declarator;
279+
280+
assert.equal(id.type, 'Identifier');
281+
assert.equal(id.typeAnnotation, null);
282+
assert.equal(id.name, 'foo');
283+
assert.equal(id.qualifier, null);
284+
285+
assert.equal(type, 'MemberExpression');
286+
assert.equal(object.name, 'x');
287+
assert.equal(property.name, 'yx');
288+
289+
});
290+
271291
it('extract type and init from implicit VariableDeclarator inside fun with multiple args', () => {
272292
const node = parse(`let x = String(() => {
273293
let foo = String(createString());

0 commit comments

Comments
 (0)