forked from sstur/js2php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path+notes.txt
78 lines (64 loc) · 2.05 KB
/
+notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
browserify tools/transform.js --standalone Transformer -o demo/js/transformer.js
js2php --modules all examples/http.js > examples/compiled/http.php
serv --spawn "php -S 127.0.0.1:8080 examples/compiled/http.php" --proxy http://127.0.0.1:8080
echo "console.log(123)" | node js2php --quiet | php
rsync -av ../js2php dev:~/
Divergence from JavaScript
get/setters don't have property descriptors
doesn't throw on use of undeclared var (ReferenceError: foo is not defined)
no runtime parsing: eval, new Function()
Not Completely Implemented
`string[1]` works but not `string['1']`
Compiling:
{a: 1} -> new Object('a', 1)
[1, 2] -> new Arr(1, 2)
/abc/i -> new RegExp('abc', 'i')
Integer literals to float: 1 -> 1.0
&& and || -> lazy-eval'd ternary `($or_ = a()) ? $or_ : b()`
operators to functions (_add, _typeof, _instanceof, _eq)
function () {} -> new Func(function() use (...) {})
[1, , 3] -> new Arr(1, Arr::$empty, 3)
for (n in o) {} -> foreach (keys($o) as $n) {}
return a(), b() -> return _seq(a(), b())
Shorthand:
can instantiate: $Array->construct()
can call functions: $func->call($context, $arg1)
can call methods: $thing->callMethod('foo', $arg1)
Double Quoted Strings:
\n \r \t \\ \$ \"
\000 or \x00
Since 5.2.5: \v = \x0B, \f = \x0C
Since 5.4: \e = \x1B
Array -> Arr
Boolean -> Bln
Buffer
Date
Error
Function -> Func
Number
Object
RegExp
String -> Str
--
Global -> GlobalObject
NaN -> NAN
Null -> Object::$null
--
Ex (exception wrapper)
------
$HTTP_RAW_POST_DATA available for all but multipart/form-data unless disabled in php.ini
------
Minimal Implementation
all variables and functions => $var
[] => array() --or-- new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS)
or use getters/setters so $arr->{0} becomes $arr[0]
{} => createStdClass()
obj.prop => $obj->prop
obj[prop] => $obj->{$prop}
Obj.prop => Obj::prop
built-in functions organized into classes like:
Math.log(num) => Math::log($num)
String.length(str) => String::length($str)
--or--
String(str).length => String::create($str)->length
$(str).length => $_($str)->length