Skip to content

Commit

Permalink
Move tuple global next to tuple value
Browse files Browse the repository at this point in the history
Reviewed By: JakobDegen

Differential Revision: D63007903

fbshipit-source-id: 19d73f933086048899851be5dcf107fc698d2f0b
  • Loading branch information
stepancheg authored and facebook-github-bot committed Sep 19, 2024
1 parent 8896b3b commit b729f5f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 37 deletions.
2 changes: 2 additions & 0 deletions starlark/src/stdlib/funcs/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ use crate::values::float::globals::register_float;
use crate::values::none::globals::register_none;
use crate::values::range::globals::register_range;
use crate::values::string::globals::register_str;
use crate::values::tuple::globals::register_tuple;
use crate::values::types::dict::globals::register_dict;
use crate::values::types::list::globals::register_list;

pub(crate) fn register_globals(globals: &mut GlobalsBuilder) {
register_list(globals);
register_tuple(globals);
register_dict(globals);
register_bool(globals);
register_none(globals);
Expand Down
37 changes: 0 additions & 37 deletions starlark/src/stdlib/funcs/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ use starlark_derive::starlark_module;
use crate as starlark;
use crate::environment::GlobalsBuilder;
use crate::eval::Evaluator;
use crate::values::function::SpecialBuiltinFunction;
use crate::values::int::PointerI32;
use crate::values::list::AllocList;
use crate::values::num::value::Num;
use crate::values::num::value::NumRef;
use crate::values::tuple::value::FrozenTuple;
use crate::values::tuple::AllocTuple;
use crate::values::tuple::TupleRef;
use crate::values::tuple::UnpackTuple;
use crate::values::types::int_or_big::StarlarkInt;
use crate::values::typing::never::StarlarkNever;
Expand Down Expand Up @@ -543,39 +539,6 @@ pub(crate) fn register_other(builder: &mut GlobalsBuilder) {
Ok(AllocList(it.into_iter().map(|x| x.0)))
}

/// [tuple](
/// https://github.com/bazelbuild/starlark/blob/master/spec.md#tuple
/// ): returns a tuple containing the elements of the iterable x.
///
/// With no arguments, `tuple()` returns the empty tuple.
///
/// ```
/// # starlark::assert::all_true(r#"
/// tuple() == ()
/// tuple([1,2,3]) == (1, 2, 3)
/// # "#);
/// ```
#[starlark(
as_type = FrozenTuple,
speculative_exec_safe,
special_builtin_function = SpecialBuiltinFunction::Tuple,
)]
fn tuple<'v>(
#[starlark(require = pos)] a: Option<ValueOfUnchecked<'v, StarlarkIter<Value<'v>>>>,
heap: &'v Heap,
) -> starlark::Result<ValueOfUnchecked<'v, &'v TupleRef<'v>>> {
if let Some(a) = a {
if TupleRef::from_value(a.get()).is_some() {
return Ok(ValueOfUnchecked::new(a.get()));
}

let it = a.get().iterate(heap)?;
Ok(ValueOfUnchecked::new(heap.alloc_tuple_iter(it)))
} else {
Ok(ValueOfUnchecked::new(heap.alloc(AllocTuple::EMPTY)))
}
}

/// [type](
/// https://github.com/bazelbuild/starlark/blob/master/spec.md#type
/// ): returns a string describing the type of its operand.
Expand Down
1 change: 1 addition & 0 deletions starlark/src/values/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//! The list type, an immutable sequence of values.

pub(crate) mod alloc;
pub(crate) mod globals;
pub(crate) mod refs;
pub(crate) mod rust_tuple;
pub(crate) mod unpack;
Expand Down
65 changes: 65 additions & 0 deletions starlark/src/values/types/tuple/globals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2018 The Starlark in Rust Authors.
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use starlark_derive::starlark_module;

use crate as starlark;
use crate::environment::GlobalsBuilder;
use crate::values::function::SpecialBuiltinFunction;
use crate::values::tuple::value::FrozenTuple;
use crate::values::tuple::AllocTuple;
use crate::values::tuple::TupleRef;
use crate::values::typing::StarlarkIter;
use crate::values::Heap;
use crate::values::Value;
use crate::values::ValueOfUnchecked;

#[starlark_module]
pub(crate) fn register_tuple(globals: &mut GlobalsBuilder) {
/// [tuple](
/// https://github.com/bazelbuild/starlark/blob/master/spec.md#tuple
/// ): returns a tuple containing the elements of the iterable x.
///
/// With no arguments, `tuple()` returns the empty tuple.
///
/// ```
/// # starlark::assert::all_true(r#"
/// tuple() == ()
/// tuple([1,2,3]) == (1, 2, 3)
/// # "#);
/// ```
#[starlark(
as_type = FrozenTuple,
speculative_exec_safe,
special_builtin_function = SpecialBuiltinFunction::Tuple,
)]
fn tuple<'v>(
#[starlark(require = pos)] a: Option<ValueOfUnchecked<'v, StarlarkIter<Value<'v>>>>,
heap: &'v Heap,
) -> starlark::Result<ValueOfUnchecked<'v, &'v TupleRef<'v>>> {
if let Some(a) = a {
if TupleRef::from_value(a.get()).is_some() {
return Ok(ValueOfUnchecked::new(a.get()));
}

let it = a.get().iterate(heap)?;
Ok(ValueOfUnchecked::new(heap.alloc_tuple_iter(it)))
} else {
Ok(ValueOfUnchecked::new(heap.alloc(AllocTuple::EMPTY)))
}
}
}

0 comments on commit b729f5f

Please sign in to comment.