Skip to content

Commit

Permalink
refactor: create a get current obj function
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Gorez <gorez.tony@gmail.com>
  • Loading branch information
tony-go committed Feb 21, 2024
1 parent c9515fe commit a93717d
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/engine/javascript_core/engine_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ static auto js_value_to_std_string(JSContextRef context, JSValueRef value)
}
}

static auto get_current_object(JSContextRef context, JSValueRef value)
-> JSObjectRef {
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(context, value, &exception);
assert(!exception);
assert(object == value);
return object;
}

auto Value::to_string() const -> std::string {
assert(is_string());
return js_value_to_std_string(this->internal->context, this->internal->value);
Expand Down Expand Up @@ -262,13 +271,11 @@ auto Value::to_map() const -> std::map<std::string, Value> {

auto Value::to_vector() const -> std::vector<Value> {
assert(is_array());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(this->internal->context,
this->internal->value, &exception);
assert(!exception);
assert(object == this->internal->value);
JSObjectRef object =
get_current_object(this->internal->context, this->internal->value);

std::vector<Value> vector;
JSValueRef exception = nullptr;
JSStringRef length_string = JSStringCreateWithUTF8CString("length");
const double length =
JSValueToNumber(this->internal->context,
Expand All @@ -294,12 +301,10 @@ auto Value::to_vector() const -> std::vector<Value> {

auto Value::at(const unsigned int &position) const -> std::optional<Value> {
assert(is_array());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(this->internal->context,
this->internal->value, &exception);
assert(!exception);
assert(object == this->internal->value);
JSObjectRef object =
get_current_object(this->internal->context, this->internal->value);

JSValueRef exception = nullptr;
JSStringRef length_string = JSStringCreateWithUTF8CString("length");
const double length =
JSValueToNumber(this->internal->context,
Expand All @@ -320,12 +325,10 @@ auto Value::at(const unsigned int &position) const -> std::optional<Value> {

auto Value::push(Value value) -> void {
assert(is_array());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(this->internal->context,
this->internal->value, &exception);
assert(!exception);
assert(object == this->internal->value);
JSObjectRef object =
get_current_object(this->internal->context, this->internal->value);

JSValueRef exception = nullptr;
JSStringRef length_string = JSStringCreateWithUTF8CString("length");
JSValueRef length_property = JSObjectGetProperty(
this->internal->context, object, length_string, &exception);
Expand Down

0 comments on commit a93717d

Please sign in to comment.