From 5febb4c6dedcca4bd20a6af16d66d2eed1a96ad6 Mon Sep 17 00:00:00 2001 From: Tony Gorez Date: Wed, 21 Feb 2024 23:58:43 +0100 Subject: [PATCH] refactor: re-order static funcs --- src/engine/javascript_core/engine_value.cc | 82 +++++++++++----------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/src/engine/javascript_core/engine_value.cc b/src/engine/javascript_core/engine_value.cc index cda14423..044a3fed 100644 --- a/src/engine/javascript_core/engine_value.cc +++ b/src/engine/javascript_core/engine_value.cc @@ -18,6 +18,47 @@ struct Value::Internal { JSValueRef value; }; +static auto js_string_to_std_string(JSStringRef value) -> std::string { + // JavaScriptCore doesn't have a function to fetch the UTF-8 byte size + // of a given string. It can only give us the amount of Unicode code-points + // in the string, which may be more than the bytes required to store it. + // As a consequence, we can't do much than allocating the maximum possible + // buffer to hold the string. + const size_t max_size = JSStringGetMaximumUTF8CStringSize(value); + std::vector buffer(max_size); + // Converts a JavaScript string into a null-terminated UTF-8 string, + // and copies the result into an external byte buffer. + JSStringGetUTF8CString(value, buffer.data(), max_size); + return {buffer.data()}; +} + +// Converting a value into a string first requires copying +// the value reference into a string reference. +static auto js_value_to_std_string(JSContextRef context, JSValueRef value) + -> std::string { + JSValueRef exception = nullptr; + JSStringRef copy = JSValueToStringCopy(context, value, &exception); + assert(!exception); + + try { + std::string result{js_string_to_std_string(copy)}; + JSStringRelease(copy); + return result; + } catch (const std::exception &) { + JSStringRelease(copy); + throw; + } +} + +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; +} + Value::Value(const void *context, const void *value) : internal{std::make_unique()} { this->internal->context = static_cast(context); @@ -87,47 +128,6 @@ auto Value::to_number() const -> double { return result; } -static auto js_string_to_std_string(JSStringRef value) -> std::string { - // JavaScriptCore doesn't have a function to fetch the UTF-8 byte size - // of a given string. It can only give us the amount of Unicode code-points - // in the string, which may be more than the bytes required to store it. - // As a consequence, we can't do much than allocating the maximum possible - // buffer to hold the string. - const size_t max_size = JSStringGetMaximumUTF8CStringSize(value); - std::vector buffer(max_size); - // Converts a JavaScript string into a null-terminated UTF-8 string, - // and copies the result into an external byte buffer. - JSStringGetUTF8CString(value, buffer.data(), max_size); - return {buffer.data()}; -} - -// Converting a value into a string first requires copying -// the value reference into a string reference. -static auto js_value_to_std_string(JSContextRef context, JSValueRef value) - -> std::string { - JSValueRef exception = nullptr; - JSStringRef copy = JSValueToStringCopy(context, value, &exception); - assert(!exception); - - try { - std::string result{js_string_to_std_string(copy)}; - JSStringRelease(copy); - return result; - } catch (const std::exception &) { - JSStringRelease(copy); - throw; - } -} - -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);