forked from rosebudai/OpenRCT2-Web
Swap Duktape for quickjs-ng
Co-authored-by: Basssiiie <Basssiiie@users.noreply.github.com> Co-authored-by: Tulio Leao <tupaschoal@gmail.com>
This commit is contained in:
1 parent
69518538fd
commit
16cd1c07ff
143 files changed
+97493
-120975
No files matched your search
@@ -34,12 +34,11 @@ namespace OpenRCT2::Scripting
|
||||
{
|
||||
constexpr size_t kColumnHeaderHeight = kListRowHeight + 1;
|
||||
|
||||
template<>
|
||||
ColumnSortOrder FromDuk(const DukValue& d)
|
||||
ColumnSortOrder ColumnSortOrderFromJS(JSContext* ctx, JSValue d)
|
||||
{
|
||||
if (d.type() == DukValue::Type::STRING)
|
||||
if (JS_IsString(d))
|
||||
{
|
||||
auto s = d.as_string();
|
||||
auto s = JSToStdString(ctx, d);
|
||||
if (s == "ascending")
|
||||
return ColumnSortOrder::Ascending;
|
||||
if (s == "descending")
|
||||
@@ -48,44 +47,36 @@ namespace OpenRCT2::Scripting
|
||||
return ColumnSortOrder::None;
|
||||
}
|
||||
|
||||
template<>
|
||||
DukValue ToDuk(duk_context* ctx, const ColumnSortOrder& value)
|
||||
static JSValue ColumnSortOrderToJS(JSContext* ctx, ColumnSortOrder value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case ColumnSortOrder::Ascending:
|
||||
return ToDuk(ctx, "ascending");
|
||||
return JSFromStdString(ctx, "ascending");
|
||||
case ColumnSortOrder::Descending:
|
||||
return ToDuk(ctx, "descending");
|
||||
return JSFromStdString(ctx, "descending");
|
||||
default:
|
||||
return ToDuk(ctx, "none");
|
||||
return JSFromStdString(ctx, "none");
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
std::optional<int32_t> FromDuk(const DukValue& d)
|
||||
{
|
||||
if (d.type() == DukValue::Type::NUMBER)
|
||||
{
|
||||
return d.as_int();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template<>
|
||||
ListViewColumn FromDuk(const DukValue& d)
|
||||
ListViewColumn ListViewColumnFromJS(JSContext* ctx, JSValue d)
|
||||
{
|
||||
ListViewColumn result;
|
||||
result.CanSort = AsOrDefault(d["canSort"], false);
|
||||
result.SortOrder = FromDuk<ColumnSortOrder>(d["sortOrder"]);
|
||||
result.Header = AsOrDefault(d["header"], "");
|
||||
result.HeaderTooltip = AsOrDefault(d["headerTooltip"], "");
|
||||
result.MinWidth = FromDuk<std::optional<int32_t>>(d["minWidth"]);
|
||||
result.MaxWidth = FromDuk<std::optional<int32_t>>(d["maxWidth"]);
|
||||
result.RatioWidth = FromDuk<std::optional<int32_t>>(d["ratioWidth"]);
|
||||
if (d["width"].type() == DukValue::Type::NUMBER)
|
||||
result.CanSort = AsOrDefault(ctx, d, "canSort", false);
|
||||
JSValue sortOrderVal = JS_GetPropertyStr(ctx, d, "sortOrder");
|
||||
result.SortOrder = ColumnSortOrderFromJS(ctx, sortOrderVal);
|
||||
JS_FreeValue(ctx, sortOrderVal);
|
||||
result.Header = AsOrDefault(ctx, d, "header", "");
|
||||
result.HeaderTooltip = AsOrDefault(ctx, d, "headerTooltip", "");
|
||||
result.MinWidth = JSToOptionalInt(ctx, d, "minWidth");
|
||||
result.MaxWidth = JSToOptionalInt(ctx, d, "maxWidth");
|
||||
result.RatioWidth = JSToOptionalInt(ctx, d, "ratioWidth");
|
||||
|
||||
auto width = JSToOptionalInt(ctx, d, "width");
|
||||
if (width.has_value())
|
||||
{
|
||||
result.MinWidth = d["width"].as_int();
|
||||
result.MinWidth = width.value();
|
||||
result.MaxWidth = result.MinWidth;
|
||||
result.RatioWidth = std::nullopt;
|
||||
}
|
||||
@@ -96,45 +87,43 @@ namespace OpenRCT2::Scripting
|
||||
return result;
|
||||
}
|
||||
|
||||
template<>
|
||||
DukValue ToDuk(duk_context* ctx, const ListViewColumn& value)
|
||||
JSValue ListViewColumnToJS(JSContext* ctx, const ListViewColumn& value)
|
||||
{
|
||||
DukObject obj(ctx);
|
||||
obj.Set("canSort", value.CanSort);
|
||||
obj.Set("sortOrder", ToDuk(ctx, value.SortOrder));
|
||||
obj.Set("header", value.Header);
|
||||
obj.Set("headerTooltip", value.HeaderTooltip);
|
||||
obj.Set("minWidth", value.MinWidth);
|
||||
obj.Set("maxWidth", value.MaxWidth);
|
||||
obj.Set("ratioWidth", value.RatioWidth);
|
||||
obj.Set("width", value.Width);
|
||||
return obj.Take();
|
||||
JSValue obj = JS_NewObject(ctx);
|
||||
JS_SetPropertyStr(ctx, obj, "canSort", JS_NewBool(ctx, value.CanSort));
|
||||
JS_SetPropertyStr(ctx, obj, "sortOrder", ColumnSortOrderToJS(ctx, value.SortOrder));
|
||||
JS_SetPropertyStr(ctx, obj, "header", JSFromStdString(ctx, value.Header));
|
||||
JS_SetPropertyStr(ctx, obj, "headerTooltip", JSFromStdString(ctx, value.HeaderTooltip));
|
||||
JS_SetPropertyStr(
|
||||
ctx, obj, "minWidth", value.MinWidth.has_value() ? JS_NewInt32(ctx, value.MinWidth.value()) : JS_NULL);
|
||||
JS_SetPropertyStr(
|
||||
ctx, obj, "maxWidth", value.MaxWidth.has_value() ? JS_NewInt32(ctx, value.MaxWidth.value()) : JS_NULL);
|
||||
JS_SetPropertyStr(
|
||||
ctx, obj, "ratioWidth", value.RatioWidth.has_value() ? JS_NewInt32(ctx, value.RatioWidth.value()) : JS_NULL);
|
||||
JS_SetPropertyStr(ctx, obj, "width", JS_NewInt32(ctx, value.Width));
|
||||
return obj;
|
||||
}
|
||||
|
||||
template<>
|
||||
ListViewItem FromDuk(const DukValue& d)
|
||||
ListViewItem ListViewItemFromJS(JSContext* ctx, JSValue d)
|
||||
{
|
||||
ListViewItem result;
|
||||
if (d.type() == DukValue::Type::STRING)
|
||||
if (JS_IsString(d))
|
||||
{
|
||||
result = ListViewItem(ProcessString(d));
|
||||
result = ListViewItem(JSToStdString(ctx, d));
|
||||
}
|
||||
else if (d.is_array())
|
||||
else if (JS_IsArray(d))
|
||||
{
|
||||
std::vector<std::string> cells;
|
||||
for (const auto& dukCell : d.as_array())
|
||||
{
|
||||
cells.push_back(ProcessString(dukCell));
|
||||
}
|
||||
JSIterateArray(ctx, d, [&cells](JSContext* ctx2, JSValue x) { cells.push_back(JSToStdString(ctx2, x)); });
|
||||
result = ListViewItem(std::move(cells));
|
||||
}
|
||||
else if (d.type() == DukValue::Type::OBJECT)
|
||||
else if (JS_IsObject(d))
|
||||
{
|
||||
auto type = ProcessString(d["type"]);
|
||||
auto type = JSToStdString(ctx, d, "type");
|
||||
// This type was misspelt between 2020 and 2025.
|
||||
if (type == "separator" || type == "seperator")
|
||||
{
|
||||
auto text = ProcessString(d["text"]);
|
||||
auto text = JSToStdString(ctx, d, "text");
|
||||
result = ListViewItem(text);
|
||||
result.IsSeparator = true;
|
||||
}
|
||||
@@ -142,64 +131,51 @@ namespace OpenRCT2::Scripting
|
||||
return result;
|
||||
}
|
||||
|
||||
template<>
|
||||
std::vector<ListViewColumn> FromDuk(const DukValue& d)
|
||||
std::vector<ListViewColumn> ListViewColumnVecFromJS(JSContext* ctx, JSValue d)
|
||||
{
|
||||
std::vector<ListViewColumn> result;
|
||||
if (d.is_array())
|
||||
if (JS_IsArray(d))
|
||||
{
|
||||
auto dukColumns = d.as_array();
|
||||
for (const auto& dukColumn : dukColumns)
|
||||
{
|
||||
result.push_back(FromDuk<ListViewColumn>(dukColumn));
|
||||
}
|
||||
JSIterateArray(ctx, d, [&result](JSContext* ctx2, JSValue x) { result.push_back(ListViewColumnFromJS(ctx2, x)); });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<>
|
||||
std::vector<ListViewItem> FromDuk(const DukValue& d)
|
||||
std::vector<ListViewItem> ListViewItemVecFromJS(JSContext* ctx, JSValue d)
|
||||
{
|
||||
std::vector<ListViewItem> result;
|
||||
if (d.is_array())
|
||||
if (JS_IsArray(d))
|
||||
{
|
||||
auto dukItems = d.as_array();
|
||||
for (const auto& dukItem : dukItems)
|
||||
{
|
||||
result.push_back(FromDuk<ListViewItem>(dukItem));
|
||||
}
|
||||
JSIterateArray(ctx, d, [&result](JSContext* ctx2, JSValue x) { result.push_back(ListViewItemFromJS(ctx2, x)); });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<>
|
||||
std::optional<RowColumn> FromDuk(const DukValue& d)
|
||||
std::optional<RowColumn> RowColumnFromJS(JSContext* ctx, JSValue d)
|
||||
{
|
||||
if (d.type() == DukValue::Type::OBJECT)
|
||||
if (JS_IsObject(d))
|
||||
{
|
||||
auto dukRow = d["row"];
|
||||
auto dukColumn = d["column"];
|
||||
if (dukRow.type() == DukValue::Type::NUMBER && dukColumn.type() == DukValue::Type::NUMBER)
|
||||
auto row = JSToOptionalInt(ctx, d, "row");
|
||||
auto column = JSToOptionalInt(ctx, d, "column");
|
||||
if (row.has_value() && column.has_value())
|
||||
{
|
||||
return RowColumn(dukRow.as_int(), dukColumn.as_int());
|
||||
return RowColumn(row.value(), column.value());
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template<>
|
||||
DukValue ToDuk(duk_context* ctx, const RowColumn& value)
|
||||
JSValue RowColumnToJS(JSContext* ctx, const RowColumn value)
|
||||
{
|
||||
DukObject obj(ctx);
|
||||
obj.Set("row", value.Row);
|
||||
obj.Set("column", value.Column);
|
||||
return obj.Take();
|
||||
JSValue obj = JS_NewObject(ctx);
|
||||
JS_SetPropertyStr(ctx, obj, "row", JS_NewInt32(ctx, value.Row));
|
||||
JS_SetPropertyStr(ctx, obj, "column", JS_NewInt32(ctx, value.Column));
|
||||
return obj;
|
||||
}
|
||||
|
||||
template<>
|
||||
ScrollbarType FromDuk(const DukValue& d)
|
||||
ScrollbarType ScrollbarTypeFromJS(JSContext* ctx, JSValue d)
|
||||
{
|
||||
auto value = AsOrDefault(d, "");
|
||||
auto value = JSToStdString(ctx, d);
|
||||
if (value == "horizontal")
|
||||
return ScrollbarType::Horizontal;
|
||||
if (value == "vertical")
|
||||
@@ -209,20 +185,19 @@ namespace OpenRCT2::Scripting
|
||||
return ScrollbarType::None;
|
||||
}
|
||||
|
||||
template<>
|
||||
DukValue ToDuk(duk_context* ctx, const ScrollbarType& value)
|
||||
JSValue ScrollbarTypeToJS(JSContext* ctx, const ScrollbarType value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
default:
|
||||
case ScrollbarType::None:
|
||||
return ToDuk(ctx, "none");
|
||||
return JSFromStdString(ctx, "none");
|
||||
case ScrollbarType::Horizontal:
|
||||
return ToDuk(ctx, "horizontal");
|
||||
return JSFromStdString(ctx, "horizontal");
|
||||
case ScrollbarType::Vertical:
|
||||
return ToDuk(ctx, "vertical");
|
||||
return JSFromStdString(ctx, "vertical");
|
||||
case ScrollbarType::Both:
|
||||
return ToDuk(ctx, "both");
|
||||
return JSFromStdString(ctx, "both");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -479,15 +454,13 @@ void CustomListView::MouseOver(const ScreenCoordsXY& pos, bool isMouseDown)
|
||||
HighlightedCell = hitResult;
|
||||
if (HighlightedCell != LastHighlightedCell)
|
||||
{
|
||||
if (hitResult->Row != kHeaderRow && OnHighlight.context() != nullptr && OnHighlight.is_function())
|
||||
if (hitResult->Row != kHeaderRow && OnHighlight.context != nullptr && OnHighlight.IsValid())
|
||||
{
|
||||
auto ctx = OnHighlight.context();
|
||||
duk_push_int(ctx, static_cast<int32_t>(HighlightedCell->Row));
|
||||
auto dukRow = DukValue::take_from_stack(ctx, -1);
|
||||
duk_push_int(ctx, static_cast<int32_t>(HighlightedCell->Column));
|
||||
auto dukColumn = DukValue::take_from_stack(ctx, -1);
|
||||
auto ctx = OnHighlight.context;
|
||||
JSValue jsRow = JS_NewInt32(ctx, HighlightedCell->Row);
|
||||
JSValue jsColumn = JS_NewInt32(ctx, HighlightedCell->Column);
|
||||
auto& scriptEngine = GetContext()->GetScriptEngine();
|
||||
scriptEngine.ExecutePluginCall(Owner, OnHighlight, { dukRow, dukColumn }, false);
|
||||
scriptEngine.ExecutePluginCall(Owner, OnHighlight.callback, { jsRow, jsColumn }, false);
|
||||
}
|
||||
Invalidate();
|
||||
}
|
||||
@@ -526,15 +499,13 @@ void CustomListView::MouseDown(const ScreenCoordsXY& pos)
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
auto ctx = OnClick.context();
|
||||
if (ctx != nullptr && OnClick.is_function())
|
||||
auto ctx = OnClick.context;
|
||||
if (ctx != nullptr && OnClick.IsValid())
|
||||
{
|
||||
duk_push_int(ctx, static_cast<int32_t>(hitResult->Row));
|
||||
auto dukRow = DukValue::take_from_stack(ctx, -1);
|
||||
duk_push_int(ctx, static_cast<int32_t>(hitResult->Column));
|
||||
auto dukColumn = DukValue::take_from_stack(ctx, -1);
|
||||
JSValue jsRow = JS_NewInt32(ctx, hitResult->Row);
|
||||
JSValue jsColumn = JS_NewInt32(ctx, hitResult->Column);
|
||||
auto& scriptEngine = GetContext()->GetScriptEngine();
|
||||
scriptEngine.ExecutePluginCall(Owner, OnClick, { dukRow, dukColumn }, false);
|
||||
scriptEngine.ExecutePluginCall(Owner, OnClick.callback, { jsRow, jsColumn }, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user