Skip to content

Commit b5caf79

Browse files
committed
Verify objects before casts
1 parent 8120650 commit b5caf79

4 files changed

Lines changed: 17 additions & 22 deletions

File tree

src/py_mini_racer/py_mini_racer.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,6 @@ def __hash__(self) -> int:
131131
return self._ctx.get_identity_hash(self)
132132

133133

134-
class JSSymbol:
135-
"""JavaScript symbol."""
136-
137-
def __init__(
138-
self,
139-
ctx: _Context,
140-
handle: _ValueHandle,
141-
):
142-
self._ctx = ctx
143-
self._handle = handle
144-
145-
146134
PythonJSConvertedTypes = Union[
147135
None,
148136
JSUndefinedType,
@@ -151,7 +139,6 @@ def __init__(
151139
float,
152140
str,
153141
JSObject,
154-
JSSymbol,
155142
datetime,
156143
memoryview,
157144
]
@@ -259,6 +246,10 @@ def __call__(
259246
return self._ctx.call_function(self, *args, this=this, timeout_sec=timeout_sec)
260247

261248

249+
class JSSymbol(JSMappedObject):
250+
"""JavaScript symbol."""
251+
252+
262253
class JSPromise(JSObject):
263254
"""JavaScript Promise.
264255
@@ -1008,10 +999,10 @@ def _wrap_raw_handle(self, raw: _RawValueHandleType) -> _ValueHandle:
1008999
return _ValueHandle(self, raw)
10091000

10101001
def _python_to_value_handle(self, obj: PythonJSConvertedTypes) -> _ValueHandle:
1011-
if isinstance(obj, (JSObject, JSSymbol)):
1012-
# JSObjects and JSSymbols originate from the V8 side. We can just send
1013-
# back the handle we originally got. (This also covers JSObject derived
1014-
# types JSFunction, JSPromise, and JSArray.)
1002+
if isinstance(obj, JSObject):
1003+
# JSObjects originate from the V8 side. We can just send back the handle
1004+
# we originally got. (This also covers derived types JSFunction, JSSymbol,
1005+
# JSPromise, and JSArray.)
10151006
return obj._handle # noqa: SLF001
10161007

10171008
if obj is None:

src/v8_py_frontend/object_manipulator.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ auto ObjectManipulator::GetIdentityHash(v8::Isolate* isolate,
3131
const v8::Context::Scope context_scope(local_context);
3232

3333
const v8::Local<v8::Value> local_obj_val = obj_ptr->ToValue(local_context);
34-
if (!local_obj_val->IsObject()) {
34+
if (!local_obj_val->IsObject() && !local_obj_val->IsSymbol()) {
3535
return bv_factory_->New("Not an object", type_execute_exception);
3636
}
3737

@@ -50,7 +50,7 @@ auto ObjectManipulator::GetOwnPropertyNames(v8::Isolate* isolate,
5050
const v8::Context::Scope context_scope(local_context);
5151

5252
const v8::Local<v8::Value> local_obj_val = obj_ptr->ToValue(local_context);
53-
if (!local_obj_val->IsObject()) {
53+
if (!local_obj_val->IsObject() && !local_obj_val->IsSymbol()) {
5454
return bv_factory_->New("Not an object", type_execute_exception);
5555
}
5656

@@ -71,7 +71,7 @@ auto ObjectManipulator::Get(v8::Isolate* isolate,
7171
const v8::Context::Scope context_scope(local_context);
7272

7373
const v8::Local<v8::Value> local_obj_val = obj_ptr->ToValue(local_context);
74-
if (!local_obj_val->IsObject()) {
74+
if (!local_obj_val->IsObject() && !local_obj_val->IsSymbol()) {
7575
return bv_factory_->New("Not an object", type_execute_exception);
7676
}
7777

@@ -98,7 +98,7 @@ auto ObjectManipulator::Set(v8::Isolate* isolate,
9898
const v8::Context::Scope context_scope(local_context);
9999

100100
const v8::Local<v8::Value> local_obj_val = obj_ptr->ToValue(local_context);
101-
if (!local_obj_val->IsObject()) {
101+
if (!local_obj_val->IsObject() && !local_obj_val->IsSymbol()) {
102102
return bv_factory_->New("Not an object", type_execute_exception);
103103
}
104104

@@ -120,7 +120,7 @@ auto ObjectManipulator::Del(v8::Isolate* isolate,
120120
const v8::Context::Scope context_scope(local_context);
121121

122122
const v8::Local<v8::Value> local_obj_val = obj_ptr->ToValue(local_context);
123-
if (!local_obj_val->IsObject()) {
123+
if (!local_obj_val->IsObject() && !local_obj_val->IsSymbol()) {
124124
return bv_factory_->New("Not an object", type_execute_exception);
125125
}
126126

tests/test_objects.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ def test_symbol(gc_check):
250250
)
251251

252252
assert isinstance(obj, JSSymbol)
253+
assert obj.__hash__()
254+
assert tuple(obj.keys()) == ()
253255

254256
del obj
255257
gc_check.check(mr)

tests/test_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ def test_symbol(gc_check):
141141
mr = MiniRacer()
142142
res = mr.eval('Symbol("my_symbol")')
143143
assert isinstance(res, JSSymbol)
144+
assert res.__hash__() is not None
145+
_test_round_trip(mr, res)
144146

145147
del res
146148
gc_check.check(mr)

0 commit comments

Comments
 (0)