forked from oraoto/pib
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathPhpBase.js
94 lines (73 loc) · 1.58 KB
/
PhpBase.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { UniqueIndex } from './UniqueIndex.js';
const STR = 'string';
const NUM = 'number';
export class PhpBase extends EventTarget
{
constructor(PhpBinary, args = {})
{
super();
const FLAGS = {};
this.onerror = function () {};
this.onoutput = function () {};
this.onready = function () {};
const callbacks = new UniqueIndex;
const targets = new UniqueIndex;
const defaults = {
callbacks, targets,
postRun: () => {
const event = new CustomEvent('ready');
this.onready(event);
this.dispatchEvent(event);
},
print: (...chunks) =>{
const event = new CustomEvent('output', {detail: chunks.map(c=>c+"\n")});
this.onoutput(event);
this.dispatchEvent(event);
},
printErr: (...chunks) => {
const event = new CustomEvent('error', {detail: chunks.map(c=>c+"\n")});
this.onerror(event);
this.dispatchEvent(event);
}
};
const phpSettings = globalThis.phpSettings || {};
this.binary = new PhpBinary(Object.assign({}, defaults, phpSettings, args)).then(php=>{
const retVal = php.ccall(
'pib_init'
, NUM
, [STR]
, []
);
return php;
}).catch(error => console.error(error));
}
run(phpCode)
{
return this.binary.then(php => php.ccall(
'pib_run'
, NUM
, [STR]
, [`?>${phpCode}`]
));
}
exec(phpCode)
{
return this.binary.then(php => php.ccall(
'pib_exec'
, STR
, [STR]
, [phpCode]
));
}
refresh()
{
const call = this.binary.then(php => php.ccall(
'pib_refresh'
, NUM
, []
, []
));
call.catch(error => console.error(error));
return call;
}
}