Skip to content

Commit

Permalink
Fix PHP 7 issues (#8)
Browse files Browse the repository at this point in the history
* Fix PHP 7 issues

* Nulll -> NullClass

* Also include RangeError, ReferenceError, SyntaxError just for consistency

* Remove package-lock.json
  • Loading branch information
Calvin-Fernandez authored and sstur committed Oct 27, 2018
1 parent 4d4b890 commit 5fad48e
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 105 deletions.
2 changes: 1 addition & 1 deletion php/classes/Array.php
Expand Up @@ -126,7 +126,7 @@ function get_length() {
function set_length($len) {
$len = self::checkInt($len);
if ($len === null) {
throw new Ex(Error::create('Invalid array length'));
throw new Ex(Err::create('Invalid array length'));
}
//when setting the length smaller than before, we need to delete elements
$oldLen = $this->length;
Expand Down
10 changes: 5 additions & 5 deletions php/classes/Buffer.php
Expand Up @@ -40,7 +40,7 @@ function init($args) {
} else if ($subject instanceof Arr) {
$this->raw = $util['arrToRaw']($subject);
} else {
throw new Ex(Error::create('Invalid parameters to construct Buffer'));
throw new Ex(Err::create('Invalid parameters to construct Buffer'));
}
$len = strlen($this->raw);
//save an integer copy of length for performance
Expand Down Expand Up @@ -80,13 +80,13 @@ static function getGlobalConstructor() {
},
'concat' => function(/*Arr*/ $list, $totalLength = null) {
if (!($list instanceof Arr)) {
throw new Ex(Error::create('Usage: Buffer.concat(array, [length])'));
throw new Ex(Err::create('Usage: Buffer.concat(array, [length])'));
}
$rawList = array();
$length = 0;
foreach ($list->toArray() as $buffer) {
if (!($buffer instanceof Buffer)) {
throw new Ex(Error::create('Usage: Buffer.concat(array, [length])'));
throw new Ex(Err::create('Usage: Buffer.concat(array, [length])'));
}
$rawList[] = $buffer->raw;
$length += $buffer->length;
Expand Down Expand Up @@ -118,15 +118,15 @@ static function getGlobalConstructor() {
$self = Func::getContext();
$i = (int)$index;
if ($i < 0 || $i >= $self->length) {
throw new Ex(Error::create('offset is out of bounds'));
throw new Ex(Err::create('offset is out of bounds'));
}
return (float)ord($self->raw[$i]);
},
'set' => function($index, $byte) {
$self = Func::getContext();
$i = (int)$index;
if ($i < 0 || $i >= $self->length) {
throw new Ex(Error::create('offset is out of bounds'));
throw new Ex(Err::create('offset is out of bounds'));
}
$old = $self->raw;
$self->raw = substr($old, 0, $i) . chr($byte) . substr($old, $i + 1);
Expand Down
78 changes: 39 additions & 39 deletions php/classes/Date.php
Expand Up @@ -189,121 +189,121 @@ static function getGlobalConstructor() {
return $date->format('D, d M Y H:i:s') . ' GMT';
},
'toDateString' => function() {
throw new Ex(Error::create('date.toDateString not implemented'));
throw new Ex(Err::create('date.toDateString not implemented'));
},
'toLocaleDateString' => function() {
throw new Ex(Error::create('date.toLocaleDateString not implemented'));
throw new Ex(Err::create('date.toLocaleDateString not implemented'));
},
'toTimeString' => function() {
throw new Ex(Error::create('date.toTimeString not implemented'));
throw new Ex(Err::create('date.toTimeString not implemented'));
},
'toLocaleTimeString' => function() {
throw new Ex(Error::create('date.toLocaleTimeString not implemented'));
throw new Ex(Err::create('date.toLocaleTimeString not implemented'));
},
'getDate' => function() {
throw new Ex(Error::create('date.getDate not implemented'));
throw new Ex(Err::create('date.getDate not implemented'));
},
'getDay' => function() {
throw new Ex(Error::create('date.getDay not implemented'));
throw new Ex(Err::create('date.getDay not implemented'));
},
'getFullYear' => function() {
throw new Ex(Error::create('date.getFullYear not implemented'));
throw new Ex(Err::create('date.getFullYear not implemented'));
},
'getHours' => function() {
throw new Ex(Error::create('date.getHours not implemented'));
throw new Ex(Err::create('date.getHours not implemented'));
},
'getMilliseconds' => function() {
throw new Ex(Error::create('date.getMilliseconds not implemented'));
throw new Ex(Err::create('date.getMilliseconds not implemented'));
},
'getMinutes' => function() {
throw new Ex(Error::create('date.getMinutes not implemented'));
throw new Ex(Err::create('date.getMinutes not implemented'));
},
'getMonth' => function() {
throw new Ex(Error::create('date.getMonth not implemented'));
throw new Ex(Err::create('date.getMonth not implemented'));
},
'getSeconds' => function() {
throw new Ex(Error::create('date.getSeconds not implemented'));
throw new Ex(Err::create('date.getSeconds not implemented'));
},
'getUTCDate' => function() {
throw new Ex(Error::create('date.getUTCDate not implemented'));
throw new Ex(Err::create('date.getUTCDate not implemented'));
},
'getUTCDay' => function() {
throw new Ex(Error::create('date.getUTCDay not implemented'));
throw new Ex(Err::create('date.getUTCDay not implemented'));
},
'getUTCFullYear' => function() {
throw new Ex(Error::create('date.getUTCFullYear not implemented'));
throw new Ex(Err::create('date.getUTCFullYear not implemented'));
},
'getUTCHours' => function() {
throw new Ex(Error::create('date.getUTCHours not implemented'));
throw new Ex(Err::create('date.getUTCHours not implemented'));
},
'getUTCMilliseconds' => function() {
throw new Ex(Error::create('date.getUTCMilliseconds not implemented'));
throw new Ex(Err::create('date.getUTCMilliseconds not implemented'));
},
'getUTCMinutes' => function() {
throw new Ex(Error::create('date.getUTCMinutes not implemented'));
throw new Ex(Err::create('date.getUTCMinutes not implemented'));
},
'getUTCMonth' => function() {
throw new Ex(Error::create('date.getUTCMonth not implemented'));
throw new Ex(Err::create('date.getUTCMonth not implemented'));
},
'getUTCSeconds' => function() {
throw new Ex(Error::create('date.getUTCSeconds not implemented'));
throw new Ex(Err::create('date.getUTCSeconds not implemented'));
},
'setDate' => function() {
throw new Ex(Error::create('date.setDate not implemented'));
throw new Ex(Err::create('date.setDate not implemented'));
},
'setFullYear' => function() {
throw new Ex(Error::create('date.setFullYear not implemented'));
throw new Ex(Err::create('date.setFullYear not implemented'));
},
'setHours' => function() {
throw new Ex(Error::create('date.setHours not implemented'));
throw new Ex(Err::create('date.setHours not implemented'));
},
'setMilliseconds' => function() {
throw new Ex(Error::create('date.setMilliseconds not implemented'));
throw new Ex(Err::create('date.setMilliseconds not implemented'));
},
'setMinutes' => function() {
throw new Ex(Error::create('date.setMinutes not implemented'));
throw new Ex(Err::create('date.setMinutes not implemented'));
},
'setMonth' => function() {
throw new Ex(Error::create('date.setMonth not implemented'));
throw new Ex(Err::create('date.setMonth not implemented'));
},
'setSeconds' => function() {
throw new Ex(Error::create('date.setSeconds not implemented'));
throw new Ex(Err::create('date.setSeconds not implemented'));
},
'setUTCDate' => function() {
throw new Ex(Error::create('date.setUTCDate not implemented'));
throw new Ex(Err::create('date.setUTCDate not implemented'));
},
'setUTCFullYear' => function() {
throw new Ex(Error::create('date.setUTCFullYear not implemented'));
throw new Ex(Err::create('date.setUTCFullYear not implemented'));
},
'setUTCHours' => function() {
throw new Ex(Error::create('date.setUTCHours not implemented'));
throw new Ex(Err::create('date.setUTCHours not implemented'));
},
'setUTCMilliseconds' => function() {
throw new Ex(Error::create('date.setUTCMilliseconds not implemented'));
throw new Ex(Err::create('date.setUTCMilliseconds not implemented'));
},
'setUTCMinutes' => function() {
throw new Ex(Error::create('date.setUTCMinutes not implemented'));
throw new Ex(Err::create('date.setUTCMinutes not implemented'));
},
'setUTCMonth' => function() {
throw new Ex(Error::create('date.setUTCMonth not implemented'));
throw new Ex(Err::create('date.setUTCMonth not implemented'));
},
'setUTCSeconds' => function() {
throw new Ex(Error::create('date.setUTCSeconds not implemented'));
throw new Ex(Err::create('date.setUTCSeconds not implemented'));
},
'getTimezoneOffset' => function() {
throw new Ex(Error::create('date.getTimezoneOffset not implemented'));
throw new Ex(Err::create('date.getTimezoneOffset not implemented'));
},
'getTime' => function() {
throw new Ex(Error::create('date.getTime not implemented'));
throw new Ex(Err::create('date.getTime not implemented'));
},
'getYear' => function() {
throw new Ex(Error::create('date.getYear not implemented'));
throw new Ex(Err::create('date.getYear not implemented'));
},
'setTime' => function() {
throw new Ex(Error::create('date.setTime not implemented'));
throw new Ex(Err::create('date.setTime not implemented'));
},
'setYear' => function() {
throw new Ex(Error::create('date.setYear not implemented'));
throw new Ex(Err::create('date.setYear not implemented'));
}
);

Expand Down
26 changes: 13 additions & 13 deletions php/classes/Error.php
@@ -1,5 +1,5 @@
<?php
class Error extends Object {
class Err extends Object {
public $className = "Error";
public $stack = null;

Expand All @@ -22,7 +22,7 @@ public function getMessage() {

//this is used in class/helper code only
static function create($str, $framesToPop = 0) {
$error = new Error($str);
$error = new Err($str);
$stack = debug_backtrace();
while ($framesToPop--) {
array_shift($stack);
Expand All @@ -37,40 +37,40 @@ static function create($str, $framesToPop = 0) {
*/
static function getGlobalConstructor() {
$Error = new Func(function($str = null) {
$error = new Error($str);
$error = new Err($str);
$error->stack = debug_backtrace();
return $error;
});
$Error->set('prototype', Error::$protoObject);
$Error->setMethods(Error::$classMethods, true, false, true);
$Error->set('prototype', Err::$protoObject);
$Error->setMethods(Err::$classMethods, true, false, true);
return $Error;
}
}

class RangeError extends Error {
class RangeErr extends Err {
public $className = "RangeError";
}

class ReferenceError extends Error {
class ReferenceErr extends Err {
public $className = "ReferenceError";
}

class SyntaxError extends Error {
class SyntaxErr extends Err {
public $className = "SyntaxError";
}

class TypeError extends Error {
class TypeErr extends Err {
public $className = "TypeError";
}

Error::$classMethods = array();
Err::$classMethods = array();

Error::$protoMethods = array(
Err::$protoMethods = array(
'toString' => function() {
$self = Func::getContext();
return $self->get('message');
}
);

Error::$protoObject = new Object();
Error::$protoObject->setMethods(Error::$protoMethods, true, false, true);
Err::$protoObject = new Object();
Err::$protoObject->setMethods(Err::$protoMethods, true, false, true);
6 changes: 3 additions & 3 deletions php/classes/Exception.php
Expand Up @@ -5,7 +5,7 @@ class Ex extends Exception {
static $errorOutputHandlers = array();

function __construct($value) {
if ($value instanceof Error) {
if ($value instanceof Err) {
$message = $value->getMessage();
} else {
$message = to_string($value);
Expand All @@ -23,7 +23,7 @@ static function handleError($level, $message, $file, $line, $context) {
if ($level === E_NOTICE) {
return false;
}
$err = Error::create($message, 1);
$err = Err::create($message, 1);
$err->set('level', $level);
throw new Ex($err);
}
Expand All @@ -38,7 +38,7 @@ static function handleException($ex) {
$output = array();
if ($ex instanceof Ex) {
$value = $ex->value;
if ($value instanceof Error) {
if ($value instanceof Err) {
$stack = $value->stack;
$frame = array_shift($stack);
if (isset($frame['file'])) {
Expand Down
4 changes: 2 additions & 2 deletions php/classes/Function.php
Expand Up @@ -167,7 +167,7 @@ static function getArguments() {
*/
static function getGlobalConstructor() {
$Function = new Func(function($fn) {
throw new Ex(Error::create('Cannot construct function at runtime.'));
throw new Ex(Err::create('Cannot construct function at runtime.'));
});
$Function->set('prototype', Func::$protoObject);
$Function->setMethods(Func::$classMethods, true, false, true);
Expand Down Expand Up @@ -253,7 +253,7 @@ static function create($callee) {
if ($args instanceof Args || $args instanceof Arr) {
$args = $args->toArray();
} else {
throw new Ex(Error::create('Function.prototype.apply: Arguments list has wrong type'));
throw new Ex(Err::create('Function.prototype.apply: Arguments list has wrong type'));
}
return $self->apply($context, $args);
},
Expand Down

1 comment on commit 5fad48e

@zaoqi
Copy link

@zaoqi zaoqi commented on 5fad48e Apr 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHP Fatal error: Cannot use 'Object' as class name as it is reserved in ....php on line 340

source https://gitlab.com/the-language/the-language/blob/dc3320656de10c0cc90656bb4f9d98341085170b/core/pure/ecmascript/lang.raw.js

#6
@cptolemy @sstur @NabiKAZ

Please sign in to comment.