Browse Source

Several changes made:

 * Do not overload entry point
 * Automatically call Program::__construct()
 * Automatically call Program::main();
 * Fix all tests
Rafal Kupiec 8 months ago
parent
commit
029dd9bfb4
Signed by: belliash <belliash@codingworkshop.eu.org> GPG Key ID: 4E829243E0CFE6B4

+ 34
- 2
engine/vm.c View File

@@ -5698,14 +5698,38 @@ static void VmInvokeShutdownCallbacks(ph7_vm *pVm) {
5698 5698
  * See block-comment on that function for additional information.
5699 5699
  */
5700 5700
 PH7_PRIVATE sxi32 PH7_VmByteCodeExec(ph7_vm *pVm) {
5701
+	ph7_class *pClass;
5702
+	ph7_class_instance *pInstance;
5703
+	ph7_class_method *pMethod;
5701 5704
 	/* Make sure we are ready to execute this program */
5702 5705
 	if(pVm->nMagic != PH7_VM_RUN) {
5703 5706
 		return pVm->nMagic == PH7_VM_EXEC ? SXERR_LOCKED /* Locked VM */ : SXERR_CORRUPT; /* Stale VM */
5704 5707
 	}
5705 5708
 	/* Set the execution magic number  */
5706 5709
 	pVm->nMagic = PH7_VM_EXEC;
5707
-	/* Execute the program */
5710
+	/* Execute the byte code */
5708 5711
 	VmByteCodeExec(&(*pVm), (VmInstr *)SySetBasePtr(pVm->pByteContainer), pVm->aOps, -1, &pVm->sExec, 0, FALSE);
5712
+	/* Extract and instantiate the entry point */
5713
+	pClass = PH7_VmExtractClass(&(*pVm), "Program", 7, TRUE /* Only loadable class but not 'interface' or 'virtual' class*/, 0);
5714
+	if(!pClass) {
5715
+		VmErrorFormat(&(*pVm), PH7_CTX_ERR, "Cannot find an entry 'Program' class");
5716
+	}
5717
+	pInstance = PH7_NewClassInstance(&(*pVm), pClass);
5718
+	if(pInstance == 0) {
5719
+		VmErrorFormat(&(*pVm), PH7_CTX_ERR, "Cannot create 'Program' instance due to a memory failure");
5720
+	}
5721
+	/* Check if a constructor is available */
5722
+	pMethod = PH7_ClassExtractMethod(pClass, "__construct", sizeof("__construct") - 1);
5723
+	if(pMethod) {
5724
+		/* Call the class constructor */
5725
+		PH7_VmCallClassMethod(&(*pVm), pInstance, pMethod, 0, 0, 0);
5726
+	}
5727
+	/* Call entry point */
5728
+	pMethod = PH7_ClassExtractMethod(pClass, "main", sizeof("main") - 1);
5729
+	if(!pMethod) {
5730
+		VmErrorFormat(&(*pVm), PH7_CTX_ERR, "Cannot find a program entry point 'Program::main()'");
5731
+	}
5732
+	PH7_VmCallClassMethod(&(*pVm), pInstance, pMethod, 0, 0, 0);
5709 5733
 	/* Invoke any shutdown callbacks */
5710 5734
 	VmInvokeShutdownCallbacks(&(*pVm));
5711 5735
 	/*
@@ -7325,6 +7349,7 @@ PH7_PRIVATE sxi32 PH7_VmCallClassMethod(
7325 7349
 ) {
7326 7350
 	ph7_value *aStack;
7327 7351
 	VmInstr aInstr[2];
7352
+	int iEntry;
7328 7353
 	int iCursor;
7329 7354
 	int i;
7330 7355
 	/* Create a new operand stack */
@@ -7344,6 +7369,7 @@ PH7_PRIVATE sxi32 PH7_VmCallClassMethod(
7344 7369
 		aStack[i].nIdx = apArg[i]->nIdx;
7345 7370
 	}
7346 7371
 	iCursor = nArg + 1;
7372
+	iEntry = 0;
7347 7373
 	if(pThis) {
7348 7374
 		/*
7349 7375
 		 * Push the class instance so that the '$this' variable will be available.
@@ -7351,6 +7377,12 @@ PH7_PRIVATE sxi32 PH7_VmCallClassMethod(
7351 7377
 		pThis->iRef++; /* Increment reference count */
7352 7378
 		aStack[i].x.pOther = pThis;
7353 7379
 		aStack[i].iFlags = MEMOBJ_OBJ;
7380
+		if(SyStrncmp(pThis->pClass->sName.zString, "Program", 7) == 0) {
7381
+			if((SyStrncmp(pMethod->sFunc.sName.zString, "main", 4) == 0) || (SyStrncmp(pMethod->sFunc.sName.zString, "__construct", 11) == 0)) {
7382
+				/* Do not overload entry point */
7383
+				iEntry = 1;
7384
+			}
7385
+		}
7354 7386
 	}
7355 7387
 	aStack[i].nIdx = SXU32_HIGH; /* Mark as constant */
7356 7388
 	i++;
@@ -7362,7 +7394,7 @@ PH7_PRIVATE sxi32 PH7_VmCallClassMethod(
7362 7394
 	/* Emit the CALL instruction */
7363 7395
 	aInstr[0].iOp = PH7_OP_CALL;
7364 7396
 	aInstr[0].iP1 = nArg; /* Total number of given arguments */
7365
-	aInstr[0].iP2 = 0;
7397
+	aInstr[0].iP2 = iEntry;
7366 7398
 	aInstr[0].p3  = 0;
7367 7399
 	/* Emit the DONE instruction */
7368 7400
 	aInstr[1].iOp = PH7_OP_DONE;

+ 2
- 4
tests/arab_to_roman.aer View File

@@ -1,4 +1,4 @@
1
-class Main {
1
+class Program {
2 2
 
3 3
 	private function num2Roman($num) {
4 4
 		$n = intval($num);
@@ -15,7 +15,7 @@ class Main {
15 15
 		return $result;
16 16
 	}
17 17
 
18
-	public function __construct() {
18
+	public function main() {
19 19
 		print('   7 => ' + $this->num2Roman(7) + "\n");	
20 20
 		print('   9 => ' + $this->num2Roman(9) + "\n");	
21 21
 		print('  11 => ' + $this->num2Roman(11) + "\n");	
@@ -28,5 +28,3 @@ class Main {
28 28
 	 }
29 29
 
30 30
 }
31
-
32
-new Main();

+ 2
- 4
tests/debug_backtrace.aer View File

@@ -1,6 +1,6 @@
1
-class Main {
1
+class Program {
2 2
 
3
-	function __construct() {
3
+	function main() {
4 4
 		$this->b($this->a('First A'), $this->a('Second A'), $this->a('Third A'));
5 5
 	}
6 6
 
@@ -19,5 +19,3 @@ class Main {
19 19
 	}
20 20
 
21 21
 }
22
-
23
-new Main();

+ 2
- 4
tests/hello_world.aer View File

@@ -1,9 +1,7 @@
1
-class Main {
1
+class Program {
2 2
 
3
-	public function __construct() {
3
+	public function main() {
4 4
 		print('Hello world!');
5 5
 	}
6 6
 
7 7
 }
8
-
9
-new Main();

+ 2
- 4
tests/load_module.aer View File

@@ -1,9 +1,7 @@
1
-final class Main {
2
-	function __construct() {
1
+final class Program {
2
+	function main() {
3 3
 		var_dump(function_exists('dummy_function'));
4 4
 		var_dump(import('dummy'));
5 5
 		var_dump(function_exists('dummy_function'));
6 6
 	}
7 7
 }
8
-
9
-new Main();

+ 2
- 4
tests/printf_test.aer View File

@@ -1,11 +1,11 @@
1
-class Main {
1
+class Program {
2 2
 	private $s = 'monkey';
3 3
 	private $t = 'many monkeys';
4 4
 	private $n =  43951789;
5 5
 	private $u = -43951789;
6 6
 	private $c = 65;
7 7
 
8
-	public function __construct() {
8
+	public function main() {
9 9
 		$this->testMonkey();
10 10
 		$this->testNumbers();
11 11
 	}
@@ -36,5 +36,3 @@ class Main {
36 36
 	}
37 37
 
38 38
 }
39
-
40
-new Main();

+ 2
- 4
tests/singleton_test.aer View File

@@ -22,13 +22,11 @@ final class Test {
22 22
 	}
23 23
 }
24 24
 
25
-final class Main {
26
-	public function __construct() {
25
+final class Program {
26
+	public function main() {
27 27
 		$testA = Test::getInstance();
28 28
 		$testA->set(5);
29 29
 		$testB = Test::getInstance();
30 30
 		var_dump($testB->get());
31 31
 	}
32 32
 } /* class */
33
-
34
-new Main();

+ 2
- 4
tests/type_juggle.aer View File

@@ -1,6 +1,6 @@
1
-class Main {
1
+class Program {
2 2
 
3
-	function __construct() {
3
+	function main() {
4 4
 		$foo = '0';
5 5
 		var_dump($foo);
6 6
 		$foo += 2;
@@ -23,5 +23,3 @@ class Main {
23 23
 		var_dump($foo);
24 24
 	}
25 25
 }
26
-
27
-new Main();

+ 2
- 4
tests/unicode_characters.aer View File

@@ -47,13 +47,11 @@ class Unicode {
47 47
 
48 48
 }
49 49
 
50
-final class Main {
50
+final class Program {
51 51
 
52
-	public function __construct() {
52
+	public function main() {
53 53
 		$unicode = new Unicode();
54 54
 		var_dump($unicode->unicon("ИфйжБЦ"));
55 55
 	}
56 56
 
57 57
 }
58
-
59
-new Main();

+ 2
- 4
tests/utf8_variables.aer View File

@@ -1,7 +1,7 @@
1
-class Main {
1
+class Program {
2 2
 	private $概要 = "AerScript Interpreter";
3 3
 
4
-	public function __construct() {
4
+	public function main() {
5 5
 		$this->ダウンロード();
6 6
 		var_dump($this->概要);
7 7
 		var_dump($this->isUTF8('hello'));
@@ -39,5 +39,3 @@ class Main {
39 39
 		return true;
40 40
 	}
41 41
 }
42
-
43
-new Main();

Loading…
Cancel
Save