Skip to content

Commit 70911f4

Browse files
list_with_capacity()
1 parent 7680899 commit 70911f4

3 files changed

Lines changed: 11 additions & 0 deletions

File tree

docs/misc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ The miscellaneous functions do not fit into any other category. They are used fo
4646
- `screenshot()`: Takes a screenshot of the current screen and saves it to the `exports` directory in the project root.
4747
- `screenshot(path)`: Takes a screenshot of the current screen and saves it to the specified path. The path is relative to the project root.
4848
- `typeof(value)`: Returns the type of the given value as a string.
49+
- `list_with_capacity(capacity)`: Returns a list with capacity for values.
4950
- `push(list, value)`: Pushes the given value to the end of the list and returns the new list.
5051
- `pop(list)`: Pops the last value from the list and returns a list containing the new list and the popped value.
5152
- `insert(list, index, value)`: Inserts the given value at the specified index in the list and returns the new list.

src/utils/sprite/builtins/builtins.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub fn builtins() -> HashMap<String, Callable> {
4747
builtin!(builtins, "set_uv", |st, ar| misc::set_uv(st, ar));
4848
builtin!(builtins, "screenshot", |st, ar| misc::screenshot(st, ar));
4949
builtin!(builtins, "typeof", |_, ar| misc::r#typeof(ar));
50+
builtin!(builtins, "list_with_capacity", |_, ar| misc::list_with_capacity(ar));
5051
builtin!(builtins, "push", |_, ar| misc::push(ar));
5152
builtin!(builtins, "pop", |_, ar| misc::pop(ar));
5253
builtin!(builtins, "insert", |_, ar| misc::insert(ar));

src/utils/sprite/builtins/misc.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,15 @@ pub fn r#typeof(args: &[Value]) -> Result {
443443
Ok(Value::String(value_type.to_string()))
444444
}
445445

446+
pub fn list_with_capacity(args: &[Value]) -> Result {
447+
if let [Value::Number(capacity)] = args {
448+
let capacity = *capacity as usize;
449+
Ok(Value::List(Vec::with_capacity(capacity)))
450+
} else {
451+
Err("list_with_capacity() expects a capacity integer".to_string())
452+
}
453+
}
454+
446455
pub fn push(args: &[Value]) -> Result {
447456
if let [Value::List(list), value] = args {
448457
let mut new_list = list.clone();

0 commit comments

Comments
 (0)