Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local Resource = world.get_type_by_name("TestResourceWithVariousFields")
local resource = world.get_resource(Resource)

assert(resource.string_map:map_get("foo") == "bar", "Expected bar, got " .. resource.string_map:map_get("foo"))
assert(resource.string_map["foo"] == "bar", "Expected bar, got " .. resource.string_map["foo"])
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let Resource = world.get_type_by_name.call("TestResourceWithVariousFields");
let resource = world.get_resource.call(Resource);

assert(resource.string_map.map_get.call("foo") == "bar", "Expected bar, got " + resource.string_map.map_get.call("foo"));
assert(resource.string_map["foo"] == "bar", "Expected bar, got " + resource.string_map["foo"]);
10 changes: 10 additions & 0 deletions assets/tests/hashmap/can_set_hashmap_value.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local Resource = world.get_type_by_name("TestResourceWithVariousFields")
local resource = world.get_resource(Resource)

resource.string_map["foo"] = "updated"
resource.string_map["new_key"] = "new_value"

local resource_changed = world.get_resource(Resource)
assert(resource_changed.string_map["foo"] == "updated", "Expected updated, got " .. resource_changed.string_map["foo"])
assert(resource_changed.string_map["new_key"] == "new_value",
"Expected new_value, got " .. resource_changed.string_map["new_key"])
10 changes: 10 additions & 0 deletions assets/tests/hashmap/can_set_hashmap_value.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let Resource = world.get_type_by_name.call("TestResourceWithVariousFields");
let resource = world.get_resource.call(Resource);


resource.string_map["foo"] = "updated";
resource.string_map["new_key"] = "new_value";

let resource_changed = world.get_resource.call(Resource);
assert(resource_changed.string_map["foo"] == "updated", "Expected updated, got " + resource_changed.string_map["foo"]);
assert(resource_changed.string_map["new_key"] == "new_value", "Expected new_value, got " + resource_changed.string_map["new_key"]);
14 changes: 14 additions & 0 deletions assets/tests/hashset/can_check_hashset_contains.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local Resource = world.get_type_by_name("TestResourceWithVariousFields")
local resource = world.get_resource(Resource)

-- Check if "apple" exists in the set (should return Some(apple))
local result = resource.string_set["apple"]
assert(result ~= nil, "Expected to find 'apple' in set")

-- Check if "banana" exists in the set (should return Some(banana))
local result2 = resource.string_set["banana"]
assert(result2 ~= nil, "Expected to find 'banana' in set")

-- Check if "nonexistent" doesn't exist in the set (should return None)
local result3 = resource.string_set["nonexistent"]
assert(result3 == nil, "Expected not to find 'nonexistent' in set")
14 changes: 14 additions & 0 deletions assets/tests/hashset/can_check_hashset_contains.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let Resource = world.get_type_by_name.call("TestResourceWithVariousFields");
let resource = world.get_resource.call(Resource);

// Check if "apple" exists in the set (should return Some(apple))
let result = resource.string_set["apple"];
assert(result != (), "Expected to find 'apple' in set");

// Check if "banana" exists in the set (should return Some(banana))
let result2 = resource.string_set["banana"];
assert(result2 != (), "Expected to find 'banana' in set");

// Check if "nonexistent" doesn't exist in the set (should return None)
let result3 = resource.string_set["nonexistent"];
assert(result3 == (), "Expected not to find 'nonexistent' in set");
19 changes: 19 additions & 0 deletions assets/tests/hashset/can_insert_into_hashset.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local Resource = world.get_type_by_name("TestResourceWithVariousFields")
local resource = world.get_resource(Resource)

-- Insert new values into the set
resource.string_set["orange"] = "orange"
resource.string_set["grape"] = "grape"

local resource_changed = world.get_resource(Resource)

-- Verify the new values were added
local orange = resource_changed.string_set["orange"]
assert(orange ~= nil, "Expected to find 'orange' in set after insertion")

local grape = resource_changed.string_set["grape"]
assert(grape ~= nil, "Expected to find 'grape' in set after insertion")

-- Verify original values are still there
local apple = resource_changed.string_set["apple"]
assert(apple ~= nil, "Expected 'apple' to still be in set")
19 changes: 19 additions & 0 deletions assets/tests/hashset/can_insert_into_hashset.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let Resource = world.get_type_by_name.call("TestResourceWithVariousFields");
let resource = world.get_resource.call(Resource);

// Insert new values into the set
resource.string_set["orange"] = "orange";
resource.string_set["grape"] = "grape";

let resource_changed = world.get_resource.call(Resource);

// Verify the new values were added
let orange = resource_changed.string_set["orange"];
assert(orange != (), "Expected to find 'orange' in set after insertion");

let grape = resource_changed.string_set["grape"];
assert(grape != (), "Expected to find 'grape' in set after insertion");

// Verify original values are still there
let apple = resource_changed.string_set["apple"];
assert(apple != (), "Expected 'apple' to still be in set");
4 changes: 2 additions & 2 deletions assets/tests/insert/vec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local res_type = world.get_type_by_name("TestResourceWithVariousFields")
local res = world.get_resource(res_type)

res.vec_usize:insert(2, 42)
res.vec_usize[2] = 42

assert(res.vec_usize[2] == 42, "insert did not work")
assert(res.vec_usize[2] == 42, "insert did not work")
2 changes: 1 addition & 1 deletion assets/tests/insert/vec.rhai
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let res_type = world.get_type_by_name.call("TestResourceWithVariousFields");
let res = world.get_resource.call(res_type);

res.vec_usize.insert.call(2, 42);
res.vec_usize[2] = 42;

assert(res.vec_usize[2] == 42, "insert did not work");
21 changes: 21 additions & 0 deletions assets/tests/iter/hashmap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local res_type = world.get_type_by_name("TestResourceWithVariousFields")
local res = world.get_resource(res_type)

local map = res.string_map

local iterator = map:iter()
local count = 0
local found_keys = {}

local result = iterator()
while result ~= nil do
local key = result[1]
local value = result[2]
count = count + 1
found_keys[key] = value
result = iterator()
end

assert(count == 2, "Expected 2 entries, got " .. count)
assert(found_keys["foo"] == "bar", "Expected foo=>bar")
assert(found_keys["zoo"] == "zed", "Expected zoo=>zed")
31 changes: 31 additions & 0 deletions assets/tests/iter/hashmap.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
let res_type = world.get_type_by_name.call("TestResourceWithVariousFields");
let res = world.get_resource.call(res_type);

let map = res.string_map;

let iterator = map.iter();
let count = 0;
let found_keys = #{};

loop {
let result = iterator.next();

if result == () {
break;
}

let key = result[0];
let value = result[1];
count += 1;
found_keys[key] = value;
}

if count != 2 {
throw `Expected 2 entries, got ${count}`;
}
if found_keys["foo"] != "bar" {
throw "Expected foo=>bar";
}
if found_keys["zoo"] != "zed" {
throw "Expected zoo=>zed";
}
22 changes: 22 additions & 0 deletions assets/tests/iter/hashmap_pairs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local res_type = world.get_type_by_name("TestResourceWithVariousFields")
local res = world.get_resource(res_type)

local map = res.string_map

local count = 0
local found_keys = {}

for key, value in pairs(map) do
count = count + 1
found_keys[key] = value
end

if count ~= 2 then
error(string.format("Expected 2 entries, got %d", count))
end
if found_keys["foo"] ~= "bar" then
error("Expected foo=>bar")
end
if found_keys["zoo"] ~= "zed" then
error("Expected zoo=>zed")
end
19 changes: 19 additions & 0 deletions assets/tests/iter/hashset.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local res_type = world.get_type_by_name("TestResourceWithVariousFields")
local res = world.get_resource(res_type)

local set = res.string_set

local iterator = set:iter()
local count = 0
local found_values = {}

local result = iterator()
while result ~= nil do
count = count + 1
found_values[result] = true
result = iterator()
end

assert(count == 2, "Expected 2 entries, got " .. count)
assert(found_values["apple"] == true, "Expected to find 'apple'")
assert(found_values["banana"] == true, "Expected to find 'banana'")
29 changes: 29 additions & 0 deletions assets/tests/iter/hashset.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let res_type = world.get_type_by_name.call("TestResourceWithVariousFields");
let res = world.get_resource.call(res_type);

let set = res.string_set;

let iterator = set.iter();
let count = 0;
let found_values = #{};

loop {
let result = iterator.next();

if result == () {
break;
}

count += 1;
found_values[result] = true;
}

if count != 2 {
throw `Expected 2 entries, got ${count}`;
}
if found_values["apple"] != true {
throw "Expected to find 'apple'";
}
if found_values["banana"] != true {
throw "Expected to find 'banana'";
}
22 changes: 22 additions & 0 deletions assets/tests/iter/hashset_pairs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local res_type = world.get_type_by_name("TestResourceWithVariousFields")
local res = world.get_resource(res_type)

local set = res.string_set

local count = 0
local found_values = {}

for value in pairs(set) do
count = count + 1
found_values[value] = true
end

if count ~= 2 then
error(string.format("Expected 2 entries, got %d", count))
end
if found_values["apple"] ~= true then
error("Expected to find 'apple'")
end
if found_values["banana"] ~= true then
error("Expected to find 'banana'")
end
14 changes: 14 additions & 0 deletions assets/tests/iter/vec_pairs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local res_type = world.get_type_by_name("TestResourceWithVariousFields")
local res = world.get_resource(res_type)

local iterated_vals = {}
for v in pairs(res.vec_usize) do
iterated_vals[#iterated_vals + 1] = v
end

assert(#iterated_vals == 5, "Length is not 5")
assert(iterated_vals[1] == 1, "First value is not 1")
assert(iterated_vals[2] == 2, "Second value is not 2")
assert(iterated_vals[3] == 3, "Third value is not 3")
assert(iterated_vals[4] == 4, "Fourth value is not 4")
assert(iterated_vals[5] == 5, "Fifth value is not 5")
32 changes: 4 additions & 28 deletions crates/bevy_mod_scripting_bindings/src/function/magic_functions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! All the switchable special functions used by language implementors
use super::{FromScriptRef, FunctionCallContext, IntoScriptRef};
use crate::{ReflectReference, ReflectionPathExt, ScriptValue, error::InteropError};
use super::FunctionCallContext;
use crate::{error::InteropError, ReflectReference, ScriptValue};
use bevy_mod_scripting_derive::DebugWithTypeInfo;
use bevy_mod_scripting_display::OrFakeId;
use bevy_reflect::{ParsedPath, PartialReflect};

/// A list of magic methods, these only have one replacable implementation, and apply to all `ReflectReferences`.
/// It's up to the language implementer to call these in the right order (after any type specific overrides).
Expand Down Expand Up @@ -51,8 +49,6 @@ impl MagicFunctions {
/// Indexes into the given reference and if the nested type is a reference type, returns a deeper reference, otherwise
/// returns the concrete value.
///
/// Does not support map types at the moment, for maps see `map_get`
///
/// Arguments:
/// * `ctxt`: The function call context.
/// * `reference`: The reference to index into.
Expand All @@ -65,13 +61,8 @@ impl MagicFunctions {
mut reference: ReflectReference,
key: ScriptValue,
) -> Result<ScriptValue, InteropError> {
let mut path: ParsedPath = key.try_into()?;
if ctxt.convert_to_0_indexed() {
path.convert_to_0_indexed();
}
reference.index_path(path);
let world = ctxt.world()?;
ReflectReference::into_script_ref(reference, world)
reference.get_indexed(key, world, ctxt.convert_to_0_indexed())
}

/// Sets the value under the specified path on the underlying value.
Expand All @@ -91,22 +82,7 @@ impl MagicFunctions {
value: ScriptValue,
) -> Result<(), InteropError> {
let world = ctxt.world()?;
let mut path: ParsedPath = key.try_into()?;
if ctxt.convert_to_0_indexed() {
path.convert_to_0_indexed();
}
reference.index_path(path);
reference.with_reflect_mut(world.clone(), |r| {
let target_type_id = r
.get_represented_type_info()
.map(|i| i.type_id())
.or_fake_id();
let other =
<Box<dyn PartialReflect>>::from_script_ref(target_type_id, value, world.clone())?;
r.try_apply(other.as_partial_reflect())
.map_err(InteropError::reflect_apply_error)?;
Ok::<_, InteropError>(())
})?
reference.set_indexed(key, value, world, ctxt.convert_to_0_indexed())
}
}

Expand Down
Loading
Loading