8000 rune: Store Vec in AnyObj instead of Mutable (relates #844) · rune-rs/rune@426e297 · GitHub
[go: up one dir, main page]

Skip to content

Commit 426e297

Browse files
committed
rune: Store Vec in AnyObj instead of Mutable (relates #844)
1 parent a122ec7 commit 426e297

File tree

9 files changed

+298
-250
lines changed

9 files changed

+298
-250
lines changed

crates/rune/src/compile/ir/interpreter.rs

Lines changed: 93 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use crate::compile::meta;
77
use crate::compile::{self, IrErrorKind, ItemId, ModId, WithSpan};
88
use crate::hir;
99
use crate::query::{Query, Used};
10-
use crate::runtime::{BorrowRefRepr, ConstValue, Mutable, Object, OwnedTuple, RefRepr, Value};
10+
use crate::runtime::{
11+
self, BorrowRefRepr, ConstValue, Mutable, Object, OwnedTuple, RefRepr, Value,
12+
};
13+
use crate::TypeHash;
1114

1215
/// The interpreter that executed [Ir][crate::ir::Ir].
1316
pub struct Interpreter<'a, 'arena> {
@@ -224,11 +227,6 @@ impl ir::Scopes {
224227
match target {
225228
BorrowRefRepr::Mutable(value) => {
226229
match &*value {
227-
Mutable::Vec(vec) => {
228-
if let Some(value) = vec.get(*index) {
229-
return Ok(value.clone());
230-
}
231-
}
232230
Mutable::Tuple(tuple) => {
233231
if let Some(value) = tuple.get(*index) {
234232
return Ok(value.clone());
@@ -242,11 +240,26 @@ impl ir::Scopes {
242240
}
243241
};
244242
}
245-
actual => {
243+
BorrowRefRepr::Any(value) => match value.type_hash() {
244+
runtime::Vec::HASH => {
245+
let vec = value.borrow_ref::<runtime::Vec>().with_span(ir_target)?;
246+
247+
if let Some(value) = vec.get(*index) {
248+
return Ok(value.clone());
249+
}
250+
}
251+
_ => {
252+
return Err(compile::Error::expected_type::<OwnedTuple>(
253+
ir_target,
254+
value.type_info(),
255+
));
256+
}
257+
},
258+
value => {
246259
return Err(compile::Error::expected_type::<OwnedTuple>(
247260
ir_target,
248-
actual.type_info(),
249-
))
261+
value.type_info(),
262+
));
250263
}
251264
}
252265

@@ -300,35 +313,47 @@ impl ir::Scopes {
300313
ir::IrTargetKind::Index(target, index) => {
301314
let target = self.get_target(target)?;
302315

303-
let mut target = match target.as_ref_repr().with_span(ir_target)? {
304-
RefRepr::Mutable(current) => current.borrow_mut().with_span(ir_target)?,
305-
actual => {
316+
match target.as_ref_repr().with_span(ir_target)? {
317+
RefRepr::Inline(value) => {
306318
return Err(compile::Error::expected_type::<OwnedTuple>(
307319
ir_target,
308-
actual.type_info().with_span(ir_target)?,
320+
value.type_info(),
309321
));
310322
}
311-
};
323+
RefRepr::Mutable(current) => {
324+
let mut mutable = current.borrow_mut().with_span(ir_target)?;
312325

313-
match &mut *target {
314-
Mutable::Vec(vec) => {
315-
if let Some(current) = vec.get_mut(*index) {
316-
*current = value;
317-
return Ok(());
318-
}
326+
match &mut *mutable {
327+
Mutable::Tuple(tuple) => {
328+
if let Some(current) = tuple.get_mut(*index) {
329+
*current = value;
330+
return Ok(());
331+
}
332+
}
333+
value => {
334+
return Err(compile::Error::expected_type::<OwnedTuple>(
335+
ir_target,
336+
value.type_info(),
337+
));
338+
}
339+
};
319340
}
320-
Mutable::Tuple(tuple) => {
321-
if let Some(current) = tuple.get_mut(*index) {
322-
*current = value;
323-
return Ok(());
341+
RefRepr::Any(any) => match any.type_hash() {
342+
runtime::Vec::HASH => {
343+
let mut vec = any.borrow_mut::<runtime::Vec>().with_span(ir_target)?;
344+
345+
if let Some(current) = vec.get_mut(*index) {
346+
*current = value;
347+
return Ok(());
348+
}
324349
}
325-
}
326-
actual => {
327-
return Err(compile::Error::expected_type::<OwnedTuple>(
328-
ir_target,
329-
actual.type_info(),
330-
));
331-
}
350+
_ => {
351+
return Err(compile::Error::expected_type::<OwnedTuple>(
352+
ir_target,
353+
any.type_info(),
354+
));
355+
}
356+
},
332357
};
333358

334359
Err(compile::Error::msg(ir_target, "missing index"))
@@ -382,40 +407,49 @@ impl ir::Scopes {
382407
ir::IrTargetKind::Index(target, index) => {
383408
let current = self.get_target(target)?;
384409

385-
let mut value = match current.as_ref_repr().with_span(ir_target)? {
386-
RefRepr::Mutable(value) => value.borrow_mut().with_span(ir_target)?,
387-
actual => {
388-
return Err(compile::Error::expected_type::<OwnedTuple>(
389-
ir_target,
390-
actual.type_info().with_span(ir_target)?,
391-
));
392-
}
393-
};
410+
match current.as_ref_repr().with_span(ir_target)? {
411+
RefRepr::Mutable(value) => {
412+
let mut value = value.borrow_mut().with_span(ir_target)?;
394413

395-
match &mut *value {
396-
Mutable::Vec(vec) => {
397-
let value = vec.get_mut(*index).ok_or_else(|| {
398-
compile::Error::new(
414+
match &mut *value {
415+
Mutable::Tuple(tuple) => {
416+
let value = tuple.get_mut(*index).ok_or_else(|| {
417+
compile::Error::new(
418+
ir_target,
419+
IrErrorKind::MissingIndex { index: *index },
420+
)
421+
})?;
422+
423+
op(value)
424+
}
425+
actual => Err(compile::Error::expected_type::<OwnedTuple>(
399426
ir_target,
400-
IrErrorKind::MissingIndex { index: *index },
401-
)
402-
})?;
403-
404-
op(value)
427+
actual.type_info(),
428+
)),
429+
}
405430
}
406-
Mutable::Tuple(tuple) => {
407-
let value = tuple.get_mut(*index).ok_or_else(|| {
408-
compile::Error::new(
409-
ir_target,
410-
IrErrorKind::MissingIndex { index: *index },
411-
)
412-
})?;
431+
RefRepr::Any(value) => match value.type_hash() {
432+
runtime::Vec::HASH => {
433+
let mut vec =
434+
value.borrow_mut::<runtime::Vec>().with_span(ir_target)?;
413435

414-
op(value)
415-
}
436+
let value = vec.get_mut(*index).ok_or_else(|| {
437+
compile::Error::new(
438+
ir_target,
439+
IrErrorKind::MissingIndex { index: *index },
440+
)
441+
})?;
442+
443+
op(value)
444+
}
445+
_ => Err(compile::Error::expected_type::<OwnedTuple>(
446+
ir_target,
447+
value.type_info(),
448+
)),
449+
},
416450
actual => Err(compile::Error::expected_type::<OwnedTuple>(
417451
ir_target,
418-
actual.type_info(),
452+
actual.type_info().with_span(ir_target)?,
419453
)),
420454
}
421455
}

crates/rune/src/modules/future.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
use crate as rune;
44
use crate::alloc::Vec;
55
use crate::runtime::{
6-
BorrowRefRepr, Future, Inline, Mut, Mutable, RefRepr, SelectFuture, Value, VmErrorKind,
6+
self, BorrowRefRepr, Future, Inline, Mut, Mutable, RefRepr, SelectFuture, Value, VmErrorKind,
77
VmResult,
88
};
9-
use crate::{ContextError, Module};
9+
use crate::{ContextError, Module, TypeHash};
1010

1111
/// Asynchronous computations.
1212
#[rune::module(::std::future)]
@@ -106,7 +106,7 @@ async fn join(value: Value) -> VmResult<Value> {
106106
Inline::Unit => VmResult::Ok(Value::unit()),
107107
value => VmResult::err([
108108
VmErrorKind::bad_argument(0),
109-
VmErrorKind::expected::<crate::runtime::Vec>(value.type_info()),
109+
VmErrorKind::expected::<runtime::Vec>(value.type_info()),
110110
]),
111111
},
112112
BorrowRefRepr::Mutable(value) => match *value {
@@ -118,21 +118,24 @@ async fn join(value: Value) -> VmResult<Value> {
118118

119119
VmResult::Ok(vm_try!(result))
120120
}
121-
Mutable::Vec(ref vec) => {
121+
ref value => VmResult::err([
122+
VmErrorKind::bad_argument(0),
123+
VmErrorKind::expected::<runtime::Vec>(value.type_info()),
124+
]),
125+
},
126+
BorrowRefRepr::Any(value) => match value.type_hash() {
127+
runtime::Vec::HASH => {
128+
let vec = vm_try!(value.borrow_ref::<runtime::Vec>());
122129
let result = try_join_impl(vec.iter(), vec.len(), |vec| {
123130
VmResult::Ok(vm_try!(Value::vec(vec)))
124131
})
125132
.await;
126133
VmResult::Ok(vm_try!(result))
127134
}
128-
ref value => VmResult::err([
135+
_ => VmResult::err([
129136
VmErrorKind::bad_argument(0),
130-
VmErrorKind::expected::<crate::runtime::Vec>(value.type_info()),
137+
VmErrorKind::expected::<runtime::Vec>(value.type_info()),
131138
]),
132139
},
133-
BorrowRefRepr::Any(value) => VmResult::err([
134-
VmErrorKind::bad_argument(0),
135-
VmErrorKind::expected::<crate::runtime::Vec>(value.type_info()),
136-
]),
137140
}
138141
}

crates/rune/src/modules/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn module() -> Result<Module, ContextError> {
8080
m.function_meta(index_get)?;
8181
m.function_meta(index_set)?;
8282
m.function_meta(resize)?;
83-
m.function_meta(string_debug)?;
83+
m.function_meta(string_debug__meta)?;
8484

8585
m.function_meta(clone__meta)?;
8686
m.implement_trait::<Vec>(rune::item!(::std::clone::Clone))?;
@@ -612,7 +612,7 @@ fn resize(this: &mut Vec, new_len: usize, value: Value) -> VmResult<()> {
612612
/// let vec = [1, 2, 3];
613613
/// assert_eq!(format!("{:?}", vec), "[1, 2, 3]");
614614
/// ```
615-
#[rune::function(instance, protocol = STRING_DEBUG)]
615+
#[rune::function(keep, instance, protocol = STRING_DEBUG)]
616616
fn string_debug(this: &Vec, f: &mut Formatter) -> VmResult<()> {
617617
Vec::string_debug_with(this, f, &mut EnvProtocolCaller)
618618
}

crates/rune/src/runtime/const_value.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use serde::{Deserialize, Serialize};
1212
use crate as rune;
1313
use crate::alloc::prelude::*;
1414
use crate::alloc::{self, HashMap};
15+
use crate::runtime;
1516
use crate::{Hash, TypeHash};
1617

1718
use super::{
@@ -130,18 +131,12 @@ impl ConstValueKind {
130131
fn type_info(&self) -> TypeInfo {
131132
match self {
132133
ConstValueKind::Inline(value) => value.type_info(),
133-
ConstValueKind::String(..) => {
134-
TypeInfo::static_type(crate::runtime::static_type::STRING)
135-
}
136-
ConstValueKind::Bytes(..) => TypeInfo::static_type(crate::runtime::static_type::BYTES),
137-
ConstValueKind::Vec(..) => TypeInfo::static_type(crate::runtime::static_type::VEC),
138-
ConstValueKind::Tuple(..) => TypeInfo::static_type(crate::runtime::static_type::TUPLE),
139-
ConstValueKind::Object(..) => {
140-
TypeInfo::static_type(crate::runtime::static_type::OBJECT)
141-
}
142-
ConstValueKind::Option(..) => {
143-
TypeInfo::static_type(crate::runtime::static_type::OPTION)
144-
}
134+
ConstValueKind::String(..) => TypeInfo::static_type(runtime::static_type::STRING),
135+
ConstValueKind::Bytes(..) => TypeInfo::static_type(runtime::static_type::BYTES),
136+
ConstValueKind::Vec(..) => TypeInfo::static_type(runtime::static_type::VEC),
137+
ConstValueKind::Tuple(..) => TypeInfo::static_type(runtime::static_type::TUPLE),
138+
ConstValueKind::Object(..) => TypeInfo::static_type(runtime::static_type::OBJECT),
139+
ConstValueKind::Option(..) => TypeInfo::static_type(runtime::static_type::OPTION),
145140
ConstValueKind::Struct(hash, ..) => TypeInfo::any_type_info(AnyTypeInfo::new(
146141
RawStr::from_str("constant struct"),
147142
*hash,
@@ -243,15 +238,6 @@ impl ConstValue {
243238
Some(some) => Some(Box::try_new(Self::from_value_ref(some)?)?),
244239
None => None,
245240
}),
246-
Mutable::Vec(ref vec) => {
247-
let mut const_vec = Vec::try_with_capacity(vec.len())?;
248-
249-
for value in vec {
250-
const_vec.try_push(Self::from_value_ref(value)?)?;
251-
}
252-
253-
ConstValueKind::Vec(const_vec)
254-
}
255241
Mutable::Tuple(ref tuple) => {
256242
let mut const_tuple = Vec::try_with_capacity(tuple.len())?;
257243

@@ -287,6 +273,16 @@ impl ConstValue {
287273
let s = value.borrow_ref::<Bytes>()?;
288274
ConstValueKind::Bytes(s.try_to_owned()?)
289275
}
276+
runtime::Vec::HASH => {
277+
let vec = value.borrow_ref::<runtime::Vec>()?;
278+
let mut const_vec = Vec::try_with_capacity(vec.len())?;
279+
280+
for value in vec.iter() {
281+
const_vec.try_push(Self::from_value_ref(value)?)?;
282+
}
283+
284+
ConstValueKind::Vec(const_vec)
285+
}
290286
_ => {
291287
return Err(RuntimeError::from(VmErrorKind::ConstNotSupported {
292288
actual: value.type_info(),

0 commit comments

Comments
 (0)
0