Skip to content

Commit 4c3953f

Browse files
committed
Set hashmap through insert
1 parent de753db commit 4c3953f

File tree

1 file changed

+19
-6
lines changed
  • crates/languages/bevy_mod_scripting_lua/src/bindings

1 file changed

+19
-6
lines changed

crates/languages/bevy_mod_scripting_lua/src/bindings/reference.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
use super::script_value::{LuaScriptValue, LUA_CALLER_CONTEXT};
1+
use std::any::TypeId;
2+
23
use bevy_mod_scripting_core::{
34
bindings::{
45
pretty_print::DisplayWithWorld, script_value::ScriptValue, ReflectReference,
56
ThreadWorldContainer, WorldContainer,
67
},
78
error::InteropError,
8-
reflection_extensions::TypeIdExtensions,
9+
reflection_extensions::{TypeIdExtensions, TypeInfoExtensions},
910
};
1011
use mlua::{MetaMethod, UserData, UserDataMethods};
11-
use std::any::TypeId;
12+
13+
use super::script_value::{LuaScriptValue, LUA_CALLER_CONTEXT};
1214

1315
/// Lua UserData wrapper for [`bevy_mod_scripting_core::bindings::ReflectReference`].
1416
/// Acts as a lua reflection interface. Any value which is registered in the type registry can be interacted with using this type.
@@ -79,13 +81,24 @@ impl UserData for LuaReflectReference {
7981
let key: ScriptValue = key.into();
8082
let value: ScriptValue = value.into();
8183
let type_id = self_.tail_type_id(world.clone())?.or_fake_id();
82-
84+
85+
let func_name = {
86+
let type_registry = world.type_registry();
87+
let type_registry = type_registry.read();
88+
let type_info = type_registry.get_type_info(type_id).ok_or_else(|| {
89+
InteropError::missing_type_data(
90+
type_id,
91+
"Type was not registered, could not determine conversion strategy."
92+
.to_owned(),
93+
)
94+
})?;
95+
if type_info.is_map() { "insert" } else { "set" }
96+
};
8397
let func = world
84-
.lookup_function([type_id, TypeId::of::<ReflectReference>()], "set")
98+
.lookup_function([type_id, TypeId::of::<ReflectReference>()], func_name)
8599
.map_err(|f| {
86100
InteropError::missing_function(TypeId::of::<ReflectReference>(), f)
87101
})?;
88-
89102
let out = func.call(
90103
vec![ScriptValue::Reference(self_), key, value],
91104
LUA_CALLER_CONTEXT,

0 commit comments

Comments
 (0)