phprs 0.1.7

A PHP interpreter with build/package manager written in Rust
Documentation
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Unit tests for virtual machine

use crate::engine::types::{PhpResult, PhpType, PhpValue, Val};
use crate::engine::vm::{
    execute_ex, get_opcode_name, temp_var_ref, var_ref, ExecuteData, Op, OpArray, Opcode,
};

fn run_php_code(code: &str) -> (PhpResult, String) {
    // Output is thread-local, so parallel tests are safe without a global lock
    let (op_array, ft) =
        crate::engine::compile::compile_string_with_functions(code, "test.php").unwrap();
    crate::php::output::php_output_start().unwrap();
    let mut ed = ExecuteData::new();
    ed.function_table = Some(std::sync::Arc::new(ft));
    let result = execute_ex(&mut ed, &op_array);
    let output = crate::php::output::php_output_end().unwrap();
    (result, output)
}

#[test]
fn test_opcode_enum() {
    // Verify opcode enum values
    assert_eq!(Opcode::Nop as u8, 0);
    assert_eq!(Opcode::Add as u8, 1);
    assert_eq!(Opcode::Sub as u8, 2);
    assert_eq!(Opcode::Mul as u8, 3);
    assert_eq!(Opcode::Div as u8, 4);
    assert_eq!(Opcode::Assign as u8, 24);
}

#[test]
fn test_op_creation() {
    let op = Op::new(
        Opcode::Add,
        Val::new(PhpValue::Long(10), PhpType::Long),
        Val::new(PhpValue::Long(20), PhpType::Long),
        Val::new(PhpValue::Long(0), PhpType::Long),
        0,
    );

    assert_eq!(op.opcode, Opcode::Add);
    assert_eq!(op.extended_value, 0);
}

#[test]
fn test_op_array_new() {
    let op_array = OpArray::new("test.php".to_string());
    assert_eq!(op_array.ops.len(), 0);
    assert_eq!(op_array.filename, Some("test.php".to_string()));
}

#[test]
fn test_get_opcode_name() {
    assert_eq!(get_opcode_name(Opcode::Nop), "NOP");
    assert_eq!(get_opcode_name(Opcode::Add), "ADD");
    assert_eq!(get_opcode_name(Opcode::Sub), "SUB");
    assert_eq!(get_opcode_name(Opcode::Mul), "MUL");
    assert_eq!(get_opcode_name(Opcode::Div), "DIV");
}

#[test]
fn test_execute_empty() {
    let op_array = OpArray::new("test.php".to_string());
    let mut execute_data = ExecuteData::new();

    let result = execute_ex(&mut execute_data, &op_array);
    // Empty op array should execute successfully
    assert!(matches!(result, PhpResult::Success));
}

#[test]
fn test_execute_data_creation() {
    let execute_data = ExecuteData::new();
    assert!(execute_data.op_array.is_none());
    assert_eq!(execute_data.current_op, 0);
}

#[test]
fn test_vm_assign_and_resolve_variable() {
    // $x = 42; — then verify $x is in symbol table
    let mut op_array = OpArray::new("test.php".to_string());
    let var_name = crate::engine::string::string_init("x", false);
    op_array.add_op(Op::new(
        Opcode::Assign,
        Val::new(PhpValue::String(Box::new(var_name)), PhpType::String),
        Val::new(PhpValue::Long(42), PhpType::Long),
        Val::new(PhpValue::Long(0), PhpType::Null),
        0,
    ));

    let mut ed = ExecuteData::new();
    let result = execute_ex(&mut ed, &op_array);
    assert!(matches!(result, PhpResult::Success));
    // Verify variable is in symbol table
    let val = ed.get_var("x");
    assert_eq!(val.get_type(), PhpType::Long);
    if let PhpValue::Long(v) = val.value {
        assert_eq!(v, 42);
    } else {
        panic!("expected Long");
    }
}

#[test]
fn test_vm_add_with_temp_vars() {
    // temp[0] = 10 + 20 → echo temp[0]
    let mut op_array = OpArray::new("test.php".to_string());
    op_array.add_op(Op::new(
        Opcode::Add,
        Val::new(PhpValue::Long(10), PhpType::Long),
        Val::new(PhpValue::Long(20), PhpType::Long),
        temp_var_ref(0),
        0,
    ));
    op_array.add_op(Op::new(
        Opcode::Echo,
        temp_var_ref(0),
        Val::new(PhpValue::Long(0), PhpType::Null),
        Val::new(PhpValue::Long(0), PhpType::Null),
        0,
    ));

    crate::php::output::php_output_start().unwrap();
    let mut ed = ExecuteData::new();
    let result = execute_ex(&mut ed, &op_array);
    let output = crate::php::output::php_output_end().unwrap();
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "30");
}

#[test]
fn test_vm_variable_in_expression() {
    // Test: $a = 5; $b = 3; echo $a + $b;
    let mut op_array = OpArray::new("test.php".to_string());
    let var_a = crate::engine::string::string_init("a", false);
    let var_b = crate::engine::string::string_init("b", false);
    // ASSIGN $a = 5
    op_array.add_op(Op::new(
        Opcode::Assign,
        Val::new(PhpValue::String(Box::new(var_a)), PhpType::String),
        Val::new(PhpValue::Long(5), PhpType::Long),
        Val::new(PhpValue::Long(0), PhpType::Null),
        0,
    ));
    // ASSIGN $b = 3
    op_array.add_op(Op::new(
        Opcode::Assign,
        Val::new(PhpValue::String(Box::new(var_b)), PhpType::String),
        Val::new(PhpValue::Long(3), PhpType::Long),
        Val::new(PhpValue::Long(0), PhpType::Null),
        0,
    ));
    // ADD var_ref("a") + var_ref("b") → temp[0]
    op_array.add_op(Op::new(
        Opcode::Add,
        var_ref("a"),
        var_ref("b"),
        temp_var_ref(0),
        0,
    ));
    // ECHO temp[0]
    op_array.add_op(Op::new(
        Opcode::Echo,
        temp_var_ref(0),
        Val::new(PhpValue::Long(0), PhpType::Null),
        Val::new(PhpValue::Long(0), PhpType::Null),
        0,
    ));

    crate::php::output::php_output_start().unwrap();
    let mut ed = ExecuteData::new();
    let result = execute_ex(&mut ed, &op_array);
    let output = crate::php::output::php_output_end().unwrap();
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "8");
}

#[test]
fn test_vm_builtin_strlen() {
    // InitFCall; SendVal "hello"; DoFCall strlen → temp[0]; Echo temp[0]
    let mut op_array = OpArray::new("test.php".to_string());
    let zero = || Val::new(PhpValue::Long(0), PhpType::Null);
    // InitFCall
    op_array.add_op(Op::new(Opcode::InitFCall, zero(), zero(), zero(), 0));
    // SendVal "hello"
    let hello = crate::engine::string::string_init("hello", false);
    op_array.add_op(Op::new(
        Opcode::SendVal,
        Val::new(PhpValue::String(Box::new(hello)), PhpType::String),
        zero(),
        zero(),
        0,
    ));
    // DoFCall strlen → temp[0]
    let fname = crate::engine::string::string_init("strlen", false);
    op_array.add_op(Op::new(
        Opcode::DoFCall,
        Val::new(PhpValue::String(Box::new(fname)), PhpType::String),
        zero(),
        temp_var_ref(0),
        0,
    ));
    // Echo temp[0]
    op_array.add_op(Op::new(Opcode::Echo, temp_var_ref(0), zero(), zero(), 0));

    crate::php::output::php_output_start().unwrap();
    let mut ed = ExecuteData::new();
    let result = execute_ex(&mut ed, &op_array);
    let output = crate::php::output::php_output_end().unwrap();
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "5");
}

#[test]
fn test_compile_and_execute_variable_arithmetic() {
    let code = r#"<?php
$x = 10;
$y = 20;
echo $x + $y;
"#;
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "30");
}

#[test]
fn test_compile_and_execute_function_call() {
    let code = r#"<?php
echo strlen("hello world");
"#;
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "11");
}

#[test]
fn test_compile_and_execute_strtoupper() {
    let code = r#"<?php
echo strtoupper("php-rs");
"#;
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "PHP-RS");
}

#[test]
fn test_compile_and_execute_complex_expression() {
    let code = r#"<?php
$a = 5;
$b = 3;
$c = $a * $b + 2;
echo $c;
"#;
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "17");
}

#[test]
fn test_compile_and_execute_string_interpolation() {
    let code = "<?php\n$name = \"World\";\necho \"Hello $name!\";\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "Hello World!");
}

#[test]
fn test_compile_and_execute_concat_operator() {
    let code = "<?php\necho \"a\" . \" \" . \"b\";\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "a b");
}

#[test]
fn test_compile_and_execute_parenthesized_expr() {
    let code = "<?php\n$x = 2;\n$y = 3;\necho ($x + $y) * 4;\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "20");
}

#[test]
fn test_compile_and_execute_function_call_statement() {
    let code = "<?php\n$x = 42;\nvar_dump($x);\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "int(42)\n");
}

#[test]
fn test_compile_and_execute_concat_with_function() {
    let code = "<?php\necho strlen(\"hello\") . \" chars\";\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "5 chars");
}

#[test]
fn test_compile_and_execute_indexed_array() {
    let code = "<?php\n$arr = [10, 20, 30];\necho count($arr);\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "3");
}

#[test]
fn test_compile_and_execute_assoc_array() {
    let code = "<?php\n$m = [\"a\" => \"hello\", \"b\" => \"world\"];\necho count($m);\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "2");
}

#[test]
fn test_compile_and_execute_array_access() {
    let code = "<?php\n$arr = [10, 20, 30];\necho $arr[0] . \" \" . $arr[2];\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "10 30");
}

#[test]
fn test_compile_and_execute_assoc_array_access() {
    let code = "<?php\n$m = [\"x\" => \"hello\"];\necho $m[\"x\"];\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "hello");
}

#[test]
fn test_compile_and_execute_ternary() {
    let code = "<?php\n$x = 10;\n$y = $x > 5 ? 'big' : 'small';\necho $y;\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "big");
}

#[test]
fn test_compile_and_execute_ternary_false() {
    let code = "<?php\n$x = 2;\n$y = $x > 5 ? 'big' : 'small';\necho $y;\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "small");
}

#[test]
fn test_compile_and_execute_null_coalesce() {
    let code = "<?php\n$x = null;\n$y = $x ?? 'default';\necho $y;\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "default");
}

#[test]
fn test_compile_and_execute_null_coalesce_non_null() {
    let code = "<?php\n$x = 'hello';\n$y = $x ?? 'default';\necho $y;\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "hello");
}

#[test]
fn test_compile_and_execute_closure_basic() {
    let code = "<?php\n$greet = function() { echo 'hi'; };\n$greet();\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "hi");
}

#[test]
fn test_compile_and_execute_closure_with_args() {
    let code = "<?php\n$add = function($a, $b) { echo $a + $b; };\n$add(3, 4);\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "7");
}

#[test]
fn test_compile_and_execute_function_return() {
    let code = "<?php\nfunction double($x) { return $x * 2; }\necho double(5);\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "10");
}

#[test]
fn test_compile_and_execute_typed_function() {
    let code = "<?php\nfunction add(int $a, int $b): int { return $a + $b; }\necho add(3, 4);\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "7");
}

#[test]
fn test_compile_and_execute_typed_closure() {
    let code = "<?php\n$fn = function(string $s): string { return $s; };\necho $fn('hello');\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "hello");
}

#[test]
fn test_compile_namespace_and_use() {
    let code = "<?php\nnamespace App\\Models;\nuse App\\Services\\Logger as Log;\necho 'ok';\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "ok");
}

#[test]
fn test_compile_trait_definition() {
    // Trait definition should parse without error
    let code =
        "<?php\ntrait Greetable {\n  public function greet() { echo 'hello'; }\n}\necho 'ok';\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "ok");
}

#[test]
fn test_compile_trait_use_in_class() {
    let code = "<?php\ntrait Greetable {\n  public function greet() { echo 'hi'; }\n}\nclass Person {\n  use Greetable;\n}\n$p = new Person();\n$p->greet();\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "hi");
}

#[test]
fn test_compile_match_expression() {
    let code = "<?php\n$val = 2;\n$result = match($val) { 1 => 'one', 2 => 'two', default => 'other', };\necho $result;\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "two");
}

#[test]
fn test_compile_attributes() {
    let code = "<?php\n#[Example]\nfunction demo() { return 'ok'; }\necho demo();\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "ok");
}

#[test]
fn test_compile_yield_statement() {
    let code = "<?php\nfunction gen() { yield 3; }\necho gen();\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "Array");
}

#[test]
fn test_compile_match_expression_complex() {
    let code = "<?php\n$val = 5;\n$base = 2;\n$result = match($val + 1) { 3 => 'low', 6 => 'ok', default => 'high', };\necho $result;\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "ok");
}

#[test]
fn test_compile_attributes_on_class_and_method() {
    let code = "<?php\n#[Entity]\nclass User {\n  #[Column]\n  public function name() { return 'alice'; }\n}\n$u = new User();\necho $u->name();\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "alice");
}

#[test]
fn test_compile_generator_multiple_yields() {
    let code =
        "<?php\nfunction gen() { yield 1; yield 2; yield 3; }\n$arr = gen();\necho $arr[1];\n";
    let (result, output) = run_php_code(code);
    assert!(matches!(result, PhpResult::Success));
    assert_eq!(output, "2");
}