Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 7f3dc3e

Browse files
committed
Add Message::getErrorLevel() method, closes #52
1 parent a8404d6 commit 7f3dc3e

6 files changed

+94
-10
lines changed

src/php_v8_message.cc

+34-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ void php_v8_message_create_from_message(zval *return_value, php_v8_isolate_t *ph
101101
if (v8::Message::kNoColumnInfo != end_column) {
102102
zend_update_property_long(this_ce, return_value, ZEND_STRL("end_column"), static_cast<zend_long>(end_column));
103103
}
104+
105+
zend_update_property_long(this_ce, return_value, ZEND_STRL("error_level"), static_cast<zend_long>(message->ErrorLevel()));
104106
}
105107

106108

@@ -117,10 +119,11 @@ static PHP_METHOD(Message, __construct) {
117119
zend_long end_position = -1;
118120
zend_long start_column = -1;
119121
zend_long end_column = -1;
122+
zend_long error_level = -1;
120123

121-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SSoSo|lllll",
124+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SSoSo|llllll",
122125
&message, &source_line, &script_origin, &resource_name, &stack_trace,
123-
&line_number, &start_position, &end_position, &start_column, &end_column) == FAILURE) {
126+
&line_number, &start_position, &end_position, &start_column, &end_column, &error_level) == FAILURE) {
124127
return;
125128
}
126129

@@ -145,6 +148,9 @@ static PHP_METHOD(Message, __construct) {
145148
if (end_column > 0) {
146149
zend_update_property_long(this_ce, getThis(), ZEND_STRL("end_column"), end_column);
147150
}
151+
if (end_column > 0) {
152+
zend_update_property_long(this_ce, getThis(), ZEND_STRL("error_level"), error_level);
153+
}
148154
}
149155

150156
static PHP_METHOD(Message, get)
@@ -257,6 +263,18 @@ static PHP_METHOD(Message, getEndColumn)
257263
RETVAL_ZVAL(zend_read_property(this_ce, getThis(), ZEND_STRL("end_column"), 0, &rv), 1, 0);
258264
}
259265

266+
static PHP_METHOD(Message, getErrorLevel)
267+
{
268+
zval rv;
269+
270+
if (zend_parse_parameters_none() == FAILURE) {
271+
return;
272+
}
273+
274+
RETVAL_ZVAL(zend_read_property(this_ce, getThis(), ZEND_STRL("error_level"), 0, &rv), 1, 0);
275+
}
276+
277+
260278
PHP_V8_ZEND_BEGIN_ARG_WITH_CONSTRUCTOR_INFO_EX(arginfo___construct, 5)
261279
ZEND_ARG_TYPE_INFO(0, message, IS_STRING, 0)
262280
ZEND_ARG_TYPE_INFO(0, source_line, IS_STRING, 0)
@@ -268,6 +286,7 @@ PHP_V8_ZEND_BEGIN_ARG_WITH_CONSTRUCTOR_INFO_EX(arginfo___construct, 5)
268286
ZEND_ARG_TYPE_INFO(0, end_position, IS_LONG, 1)
269287
ZEND_ARG_TYPE_INFO(0, start_column, IS_LONG, 1)
270288
ZEND_ARG_TYPE_INFO(0, end_column, IS_LONG, 1)
289+
ZEND_ARG_TYPE_INFO(0, error_level, IS_LONG, 1)
271290
ZEND_END_ARG_INFO()
272291

273292
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_get, ZEND_RETURN_VALUE, 0, IS_STRING, 0)
@@ -300,6 +319,9 @@ ZEND_END_ARG_INFO()
300319
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_getEndColumn, ZEND_RETURN_VALUE, 0, IS_LONG, 1)
301320
ZEND_END_ARG_INFO()
302321

322+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_getErrorLevel, ZEND_RETURN_VALUE, 0, IS_LONG, 1)
323+
ZEND_END_ARG_INFO()
324+
303325

304326
static const zend_function_entry php_v8_message_methods[] = {
305327
PHP_V8_ME(Message, __construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
@@ -313,15 +335,24 @@ static const zend_function_entry php_v8_message_methods[] = {
313335
PHP_V8_ME(Message, getEndPosition, ZEND_ACC_PUBLIC)
314336
PHP_V8_ME(Message, getStartColumn, ZEND_ACC_PUBLIC)
315337
PHP_V8_ME(Message, getEndColumn, ZEND_ACC_PUBLIC)
338+
PHP_V8_ME(Message, getErrorLevel, ZEND_ACC_PUBLIC)
316339

317340
PHP_FE_END
318341
};
319342

343+
320344
PHP_MINIT_FUNCTION (php_v8_message) {
321345
zend_class_entry ce;
322346
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "Message", php_v8_message_methods);
323347
this_ce = zend_register_internal_class(&ce);
324348

349+
zend_declare_class_constant_long(this_ce, ZEND_STRL("ERROR_LEVEL_LOG"), v8::Isolate::MessageErrorLevel::kMessageLog);
350+
zend_declare_class_constant_long(this_ce, ZEND_STRL("ERROR_LEVEL_DEBUG"), v8::Isolate::MessageErrorLevel::kMessageDebug);
351+
zend_declare_class_constant_long(this_ce, ZEND_STRL("ERROR_LEVEL_INFO"), v8::Isolate::MessageErrorLevel::kMessageInfo);
352+
zend_declare_class_constant_long(this_ce, ZEND_STRL("ERROR_LEVEL_ERROR"), v8::Isolate::MessageErrorLevel::kMessageError);
353+
zend_declare_class_constant_long(this_ce, ZEND_STRL("ERROR_LEVEL_WARNING"), v8::Isolate::MessageErrorLevel::kMessageWarning);
354+
zend_declare_class_constant_long(this_ce, ZEND_STRL("ERROR_LEVEL_ALL"), v8::Isolate::MessageErrorLevel::kMessageAll);
355+
325356
zend_declare_property_string(this_ce, ZEND_STRL("message"), "", ZEND_ACC_PRIVATE);
326357
zend_declare_property_null(this_ce, ZEND_STRL("script_origin"), ZEND_ACC_PRIVATE);
327358
zend_declare_property_string(this_ce, ZEND_STRL("source_line"), "", ZEND_ACC_PRIVATE);
@@ -334,6 +365,7 @@ PHP_MINIT_FUNCTION (php_v8_message) {
334365

335366
zend_declare_property_null(this_ce, ZEND_STRL("start_column"), ZEND_ACC_PRIVATE);
336367
zend_declare_property_null(this_ce, ZEND_STRL("end_column"), ZEND_ACC_PRIVATE);
368+
zend_declare_property_null(this_ce, ZEND_STRL("error_level"), ZEND_ACC_PRIVATE);
337369

338370
return SUCCESS;
339371
}

stubs/src/Message.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
*/
2121
class Message
2222
{
23+
const ERROR_LEVEL_LOG = 1;
24+
const ERROR_LEVEL_DEBUG = 2;
25+
const ERROR_LEVEL_INFO = 4;
26+
const ERROR_LEVEL_ERROR = 8;
27+
const ERROR_LEVEL_WARNING = 16;
28+
const ERROR_LEVEL_ALL = 31;
29+
2330
/**
2431
* @var ScriptOrigin
2532
*/
@@ -60,6 +67,10 @@ class Message
6067
* @var int|null
6168
*/
6269
private $end_column;
70+
/**
71+
* @var int|null
72+
*/
73+
private $error_level;
6374

6475
/**
6576
* @param string $message
@@ -72,6 +83,7 @@ class Message
7283
* @param int $end_position
7384
* @param int $start_column
7485
* @param int $end_column
86+
* @param int|null $error_level
7587
*/
7688
public function __construct(
7789
string $message,
@@ -83,7 +95,8 @@ public function __construct(
8395
?int $start_position = null,
8496
?int $end_position = null,
8597
?int $start_column = null,
86-
?int $end_column = null
98+
?int $end_column = null,
99+
?int $error_level = null
87100
) {
88101
}
89102

@@ -181,4 +194,13 @@ public function getStartColumn(): ?int
181194
public function getEndColumn(): ?int
182195
{
183196
}
197+
198+
/**
199+
* Returns the error level of the message.
200+
*
201+
* @return int|null
202+
*/
203+
public function getErrorLevel(): ?int
204+
{
205+
}
184206
}

tests/001-verify_extension_entities.phpt

+9-1
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,12 @@ class V8\TryCatch
440440
public function getExternalException(): ?Throwable
441441

442442
class V8\Message
443+
const ERROR_LEVEL_LOG = 1
444+
const ERROR_LEVEL_DEBUG = 2
445+
const ERROR_LEVEL_INFO = 4
446+
const ERROR_LEVEL_ERROR = 8
447+
const ERROR_LEVEL_WARNING = 16
448+
const ERROR_LEVEL_ALL = 31
443449
private $message
444450
private $script_origin
445451
private $source_line
@@ -450,7 +456,8 @@ class V8\Message
450456
private $end_position
451457
private $start_column
452458
private $end_column
453-
public function __construct(string $message, string $source_line, V8\ScriptOrigin $script_origin, string $resource_name, V8\StackTrace $stack_trace, ?int $line_number, ?int $start_position, ?int $end_position, ?int $start_column, ?int $end_column)
459+
private $error_level
460+
public function __construct(string $message, string $source_line, V8\ScriptOrigin $script_origin, string $resource_name, V8\StackTrace $stack_trace, ?int $line_number, ?int $start_position, ?int $end_position, ?int $start_column, ?int $end_column, ?int $error_level)
454461
public function get(): string
455462
public function getSourceLine(): string
456463
public function getScriptOrigin(): V8\ScriptOrigin
@@ -461,6 +468,7 @@ class V8\Message
461468
public function getEndPosition(): ?int
462469
public function getStartColumn(): ?int
463470
public function getEndColumn(): ?int
471+
public function getErrorLevel(): ?int
464472

465473
class V8\StackFrame
466474
private $line_number

tests/ExceptionManager_createCreateMessage.phpt

+4-2
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,14 @@ V8\Message->getStartPosition(): int(231)
114114
V8\Message->getEndPosition(): int(232)
115115
V8\Message->getStartColumn(): int(8)
116116
V8\Message->getEndColumn(): int(9)
117+
V8\Message->getErrorLevel(): int(8)
117118

118119
Message created from created value:
119120
-----------------------------------
120121
V8\Message->get(): string(13) "Uncaught test"
121122
V8\Message->getSourceLine(): string(24) " test(exception);"
122123
V8\Message->getScriptOrigin():
123-
object(V8\ScriptOrigin)#33 (6) {
124+
object(V8\ScriptOrigin)#34 (6) {
124125
["resource_name":"V8\ScriptOrigin":private]=>
125126
string(7) "test.js"
126127
["resource_line_offset":"V8\ScriptOrigin":private]=>
@@ -132,7 +133,7 @@ V8\Message->getScriptOrigin():
132133
["source_map_url":"V8\ScriptOrigin":private]=>
133134
string(0) ""
134135
["options":"V8\ScriptOrigin":private]=>
135-
object(V8\ScriptOriginOptions)#32 (1) {
136+
object(V8\ScriptOriginOptions)#33 (1) {
136137
["flags":"V8\ScriptOriginOptions":private]=>
137138
int(0)
138139
}
@@ -144,6 +145,7 @@ V8\Message->getStartPosition(): int(231)
144145
V8\Message->getEndPosition(): int(232)
145146
V8\Message->getStartColumn(): int(8)
146147
V8\Message->getEndColumn(): int(9)
148+
V8\Message->getErrorLevel(): int(8)
147149

148150

149151
Checks on V8\ObjectValue:

tests/Message.phpt

+21-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ $helper->header('Object representation (default)');
2222
$helper->dump($obj);
2323
$helper->space();
2424

25+
$helper->header('Class constants');
26+
$helper->dump_object_constants($obj);
27+
$helper->space();
28+
2529
$helper->header('Test getters (default)');
2630
$helper->method_matches_with_output($obj, 'get', 'message');
2731
$helper->method_matches_with_output($obj, 'getSourceLine', 'source_line');
@@ -36,7 +40,7 @@ $helper->method_matches_with_output($obj, 'getEndColumn', null);
3640
$helper->space();
3741

3842

39-
$obj = new V8\Message('message', 'source_line', $origin, 'resource_name', $trace, 1, 2, 3, 4, 5);
43+
$obj = new V8\Message('message', 'source_line', $origin, 'resource_name', $trace, 1, 2, 3, 4, 5, 7);
4044

4145
$helper->header('Object representation');
4246
$helper->dump($obj);
@@ -59,7 +63,7 @@ $helper->space();
5963
--EXPECT--
6064
Object representation (default):
6165
--------------------------------
62-
object(V8\Message)#7 (10) {
66+
object(V8\Message)#7 (11) {
6367
["message":"V8\Message":private]=>
6468
string(7) "message"
6569
["script_origin":"V8\Message":private]=>
@@ -100,9 +104,21 @@ object(V8\Message)#7 (10) {
100104
NULL
101105
["end_column":"V8\Message":private]=>
102106
NULL
107+
["error_level":"V8\Message":private]=>
108+
NULL
103109
}
104110

105111

112+
Class constants:
113+
----------------
114+
V8\Message::ERROR_LEVEL_LOG = 1
115+
V8\Message::ERROR_LEVEL_DEBUG = 2
116+
V8\Message::ERROR_LEVEL_INFO = 4
117+
V8\Message::ERROR_LEVEL_ERROR = 8
118+
V8\Message::ERROR_LEVEL_WARNING = 16
119+
V8\Message::ERROR_LEVEL_ALL = 31
120+
121+
106122
Test getters (default):
107123
-----------------------
108124
V8\Message::get() matches expected 'message'
@@ -119,7 +135,7 @@ V8\Message::getEndColumn() matches expected NULL
119135

120136
Object representation:
121137
----------------------
122-
object(V8\Message)#8 (10) {
138+
object(V8\Message)#8 (11) {
123139
["message":"V8\Message":private]=>
124140
string(7) "message"
125141
["script_origin":"V8\Message":private]=>
@@ -160,6 +176,8 @@ object(V8\Message)#8 (10) {
160176
int(4)
161177
["end_column":"V8\Message":private]=>
162178
int(5)
179+
["error_level":"V8\Message":private]=>
180+
int(7)
163181
}
164182

165183

tests/TryCatch.phpt

+3-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ object(V8\TryCatch)#11 (8) {
142142
}
143143
}
144144
["message":"V8\TryCatch":private]=>
145-
object(V8\Message)#6 (10) {
145+
object(V8\Message)#6 (11) {
146146
["message":"V8\Message":private]=>
147147
string(7) "message"
148148
["script_origin":"V8\Message":private]=>
@@ -183,6 +183,8 @@ object(V8\TryCatch)#11 (8) {
183183
NULL
184184
["end_column":"V8\Message":private]=>
185185
NULL
186+
["error_level":"V8\Message":private]=>
187+
NULL
186188
}
187189
["can_continue":"V8\TryCatch":private]=>
188190
bool(true)

0 commit comments

Comments
 (0)