Skip to content

Commit 6c9c03f

Browse files
committed
Provide Math polyfill bundle and allow to render large codes
1 parent 3ee0fdf commit 6c9c03f

File tree

5 files changed

+179
-3
lines changed

5 files changed

+179
-3
lines changed

examples/math.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var randomOk = true;
2+
var changed = false;
3+
var lastNumber = null;
4+
5+
for (var i = 0; i < 50; i++) {
6+
var rand = Math.random();
7+
if (lastNumber && lastNumber !== rand) {
8+
changed = true;
9+
}
10+
lastNumber = rand;
11+
if (rand < 0 || rand >= 1) {
12+
randomOk = false;
13+
}
14+
}
15+
16+
return (randomOk && changed) ? (Math.round(Math.min(25, 38) / Math.max(4, 10)) + Math.floor(4.6) - Math.ceil(4.6)) : -1;

examples/math.return

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2

src/JsPhpize/Compiler/Compiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ protected function handleVariableChildren(DynamicValue $dynamicValue, $indent, $
296296
protected function visitVariable(Variable $variable, $indent)
297297
{
298298
$name = $variable->name;
299-
if ($name === 'RegExp') {
300-
$this->requireHelper('regExpClass');
299+
if (in_array($name, ['Math', 'RegExp'])) {
300+
$this->requireHelper(lcfirst($name) . 'Class');
301301
}
302302
if ($variable->scope) {
303303
$name = '__let_' . spl_object_hash($variable->scope) . $name;
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
isset($Math) ? $Math : ($Math = [
2+
'DEG_PER_RAD' => M_PI / 180,
3+
'E' => M_E,
4+
'LN2' => M_LN2,
5+
'LN10' => M_LN10,
6+
'LOG2E' => M_LOG2E,
7+
'LOG10E' => M_LOG10E,
8+
'PI' => M_PI,
9+
'RAD_PER_DEG' => 180 / M_PI,
10+
'SQRT1_2' => M_SQRT1_2,
11+
'SQRT2' => M_SQRT2,
12+
13+
'abs' => 'abs',
14+
'acos' => 'acos',
15+
'acosh' => 'acosh',
16+
'asin' => 'asin',
17+
'asinh' => 'asinh',
18+
'atan' => 'atan',
19+
'atan2' => 'atan2',
20+
'atanh' => 'atanh',
21+
'cbrt' => function ($value) {
22+
return pow($value, 1 / 3);
23+
},
24+
'ceil' => 'ceil',
25+
'clamp' => function ($value, $min, $max) {
26+
return max($min, min($value, $max));
27+
},
28+
'clz32' => function ($value) {
29+
if ($value === -INF) {
30+
return 32;
31+
}
32+
if ($value < 0 || ($value |= 0) < 0) {
33+
return 0;
34+
}
35+
return 32 - ceil(log(1 + $value, 2));
36+
},
37+
'cos' => 'cos',
38+
'cosh' => 'cosh',
39+
'degrees' => 'rad2deg',
40+
'exp' => 'exp',
41+
'expm1' => 'expm1',
42+
'floor' => 'floor',
43+
'fround' => function ($value) {
44+
return unpack('f', pack('f', $value))[1];
45+
},
46+
'fscale' => function ($value, $inLow, $inHigh, $outLow, $outHigh) {
47+
if (in_array(NAN, [$value, $inLow, $inHigh, $outLow, $outHigh])) {
48+
return NAN;
49+
}
50+
return unpack('f', pack('f', (
51+
$value === INF || $value === -INF
52+
? $value
53+
: (($value - $inLow) * ($inHigh - $outLow) / ($inHigh - $inLow) + $outLow)
54+
)))[1];
55+
},
56+
'hypot' => function ($value1, $value2) {
57+
return sqrt(array_sum(array_map(function ($number) {
58+
return $number * $number;
59+
}, func_get_args())));
60+
},
61+
'iaddh' => '',
62+
'imul' => function ($x, $y) {
63+
return ($x | 0) * ($y | 0) | 0;
64+
},
65+
'imulh' => function ($u, $v) {
66+
$urShift = function ($n, $s) {
67+
return ($n >= 0) ? ($n >> $s) :
68+
(($n & 0x7fffffff) >> $s) |
69+
(0x40000000 >> ($s - 1));
70+
};
71+
$UINT16 = 0xffff;
72+
$u = +$u;
73+
$v = +$v;
74+
$u0 = $u & $UINT16;
75+
$v0 = $v & $UINT16;
76+
$u1 = $u >> 16;
77+
$v1 = $v >> 16;
78+
$t = ($u1 * ($urShift($v0, 1) << 1)) + $urShift($u0 * $v0, 16);
79+
80+
return $u1 * $v1 + ($t >> 16) + (($urShift($u0 * $v1, 1) << 1) + ($t & $UINT16) >> 16);
81+
},
82+
'isubh' => function ($x0, $x1, $y0, $y1) {
83+
$urShift = function ($n, $s) {
84+
return ($n >= 0) ? ($n >> $s) :
85+
(($n & 0x7fffffff) >> $s) |
86+
(0x40000000 >> ($s - 1));
87+
};
88+
$x0 = $urShift($x0, 1) << 1;
89+
$x1 = $urShift($x1, 1) << 1;
90+
$y0 = $urShift($y0, 1) << 1;
91+
92+
return $x1 - ($urShift($y1, 1) << 1) - $urShift((~$x0 & $y0 | ~($x0 ^ $y0) & $x0 - ($urShift($y0, 1) << 1)), 31) | 0;
93+
},
94+
'log' => 'log',
95+
'log1p' => function ($value) {
96+
return log(1 + $value);
97+
},
98+
'log2' => function ($value) {
99+
return log($value, 2);
100+
},
101+
'log10' => 'log10',
102+
'max' => 'max',
103+
'min' => 'min',
104+
'pow' => 'pow',
105+
'radians' => 'deg2rad',
106+
'random' => function () {
107+
return mt_rand() / (mt_getrandmax() + 1);
108+
},
109+
'round' => 'round',
110+
'scale' => function ($value, $inLow, $inHigh, $outLow, $outHigh) {
111+
if (in_array(NAN, [$value, $inLow, $inHigh, $outLow, $outHigh])) {
112+
return NAN;
113+
}
114+
if ($value === INF || $value === -INF) {
115+
return $value;
116+
}
117+
return ($value - $inLow) * ($inHigh - $outLow) / ($inHigh - $inLow) + $outLow;
118+
},
119+
'sign' => function ($value) {
120+
return ($value > 0 ? 1 : ($value < 0 ? -1 : 0));
121+
},
122+
'signbit' => function ($value) {
123+
return $value >= 0;
124+
},
125+
'sin' => 'sin',
126+
'sinh' => 'sinh',
127+
'sqrt' => 'sqrt',
128+
'tan' => 'tan',
129+
'tanh' => 'tanh',
130+
'trunc' => function ($value) {
131+
return $value >= 0 ? floor($value) : ceil($value);
132+
},
133+
'umulh' => function ($u, $v) {
134+
$urShift = function ($n, $s) {
135+
return ($n >= 0) ? ($n >> $s) :
136+
(($n & 0x7fffffff) >> $s) |
137+
(0x40000000 >> ($s - 1));
138+
};
139+
$UINT16 = 0xffff;
140+
$u = +$u;
141+
$v = +$v;
142+
$u0 = $u & $UINT16;
143+
$v0 = $v & $UINT16;
144+
$u1 = $urShift($u, 16);
145+
$v1 = $urShift($v, 16);
146+
$t = ($u1 * ($urShift($v0, 1) << 1)) + $urShift($u0 * $v0, 16);
147+
148+
return $u1 * $v1 + $urShift($t, 16) + $urShift(($urShift($u0 * $v1, 1) << 1) + ($t & $UINT16), 16);
149+
},
150+
])

src/JsPhpize/JsPhpize.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,16 @@ public function render($input, $filename = null, array $variables = [])
155155
extract(array_merge($this->sharedVariables, $variables));
156156

157157
try {
158-
return include $this->stream . '://data;<?php ' . $this->compile($input, $filename);
158+
$code = '<?php ' . $this->compile($input, $filename);
159+
if (strlen($code) < 4096) {
160+
return include $this->stream . '://data;' . $code;
161+
}
162+
$file = tempnam(sys_get_temp_dir(), 'jsph');
163+
file_put_contents($file, $code);
164+
$result = include $file;
165+
unlink($file);
166+
167+
return $result;
159168
} catch (\JsPhpize\Compiler\Exception $exception) {
160169
throw $exception;
161170
} catch (\JsPhpize\Lexer\Exception $exception) {

0 commit comments

Comments
 (0)