Skip to content

Commit 7d35881

Browse files
JakobDegenfacebook-github-bot
authored andcommitted
module: Explicit virtualization of NativeMeth and NativeFunc
Summary: Like we did a couple diffs ago for attributes, replace the `&dyn NativeMeth` with a `NativeMeth` that stores a function pointer and the one data field that's needed As a part of that, on the macro side, stop emitting a struct and just generate functions Reviewed By: Nero5023 Differential Revision: D73726006 fbshipit-source-id: af0f2450c334fb7202fdbc38b7e118e4e77555a3
1 parent d5ffb22 commit 7d35881

25 files changed

+162
-190
lines changed

starlark/src/environment/globals.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::docs::DocModule;
3131
use crate::docs::DocString;
3232
use crate::docs::DocStringKind;
3333
use crate::docs::DocType;
34+
use crate::eval::ParametersSpec;
3435
use crate::stdlib;
3536
pub use crate::stdlib::LibraryExtension;
3637
use crate::typing::Ty;
@@ -41,6 +42,7 @@ use crate::values::FrozenStringValue;
4142
use crate::values::FrozenValue;
4243
use crate::values::Value;
4344
use crate::values::function::NativeFunc;
45+
use crate::values::function::NativeFuncFn;
4446
use crate::values::function::SpecialBuiltinFunction;
4547
use crate::values::namespace::FrozenNamespace;
4648
use crate::values::namespace::value::MaybeDocHiddenValue;
@@ -282,21 +284,20 @@ impl GlobalsBuilder {
282284

283285
/// Set a method. This function is usually called from code
284286
/// generated by `starlark_derive` and rarely needs to be called manually.
285-
pub fn set_function<F>(
287+
pub fn set_function(
286288
&mut self,
287289
name: &str,
288290
components: NativeCallableComponents,
291+
sig: ParametersSpec<FrozenValue>,
289292
as_type: Option<(Ty, DocType)>,
290293
ty: Option<Ty>,
291294
special_builtin_function: Option<SpecialBuiltinFunction>,
292-
f: F,
293-
) where
294-
F: NativeFunc,
295-
{
295+
f: NativeFuncFn,
296+
) {
296297
self.set(
297298
name,
298299
NativeFunction {
299-
function: Box::new(f),
300+
function: NativeFunc(f, sig),
300301
name: name.to_owned(),
301302
speculative_exec_safe: components.speculative_exec_safe,
302303
as_type: as_type.as_ref().map(|x| x.0.dupe()),

starlark/src/environment/methods.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ use crate::collections::symbol::map::SymbolMap;
2424
use crate::collections::symbol::symbol::Symbol;
2525
use crate::docs::DocType;
2626
use crate::environment::common_documentation;
27+
use crate::eval::ParametersSpec;
2728
use crate::typing::Ty;
2829
use crate::values::AllocFrozenValue;
2930
use crate::values::FrozenHeap;
3031
use crate::values::FrozenHeapRef;
31-
use crate::values::FrozenRef;
3232
use crate::values::FrozenValue;
3333
use crate::values::Heap;
3434
use crate::values::Value;
3535
use crate::values::function::NativeAttribute;
3636
use crate::values::function::NativeMeth;
37+
use crate::values::function::NativeMethFn;
3738
use crate::values::function::NativeMethod;
3839
use crate::values::types::unbound::UnboundValue;
3940

@@ -205,22 +206,22 @@ impl MethodsBuilder {
205206
);
206207
}
207208

208-
/// Set a method. This function is usually called from code
209-
/// generated by `starlark_derive` and rarely needs to be called manually.
210-
pub fn set_method<F>(&mut self, name: &str, components: NativeCallableComponents, f: F)
211-
where
212-
F: NativeMeth,
213-
{
209+
/// Set a method. Only used by `starlark_module` macro
210+
#[doc(hidden)]
211+
pub fn set_method(
212+
&mut self,
213+
name: &str,
214+
components: NativeCallableComponents,
215+
sig: ParametersSpec<FrozenValue>,
216+
f: NativeMethFn,
217+
) {
214218
// TODO(nga): do not unwrap.
215219
let ty = Ty::from_native_callable_components(&components, None).unwrap();
216220

217-
let function = FrozenRef::<dyn NativeMeth + 'static>::new(
218-
self.heap.alloc_any_debug_type_name(f).as_ref(),
219-
);
220221
self.members.insert(
221222
name,
222223
UnboundValue::Method(self.heap.alloc_simple_typed(NativeMethod {
223-
function,
224+
function: NativeMeth(f, sig),
224225
name: name.to_owned(),
225226
speculative_exec_safe: components.speculative_exec_safe,
226227
docs: components.into_docs(None),

starlark/src/eval/bc/native_function.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use dupe::Dupe;
2020

2121
use crate::eval::Arguments;
2222
use crate::eval::Evaluator;
23-
use crate::values::FrozenRef;
2423
use crate::values::FrozenValueTyped;
2524
use crate::values::Value;
2625
use crate::values::function::NativeFunc;
@@ -31,13 +30,15 @@ use crate::values::function::NativeFunction;
3130
pub(crate) struct BcNativeFunction {
3231
fun: FrozenValueTyped<'static, NativeFunction>,
3332
/// Copy function here from `fun` to avoid extra dereference when calling.
34-
imp: FrozenRef<'static, dyn NativeFunc>,
33+
imp: &'static NativeFunc,
3534
}
3635

3736
impl BcNativeFunction {
3837
pub(crate) fn new(fun: FrozenValueTyped<'static, NativeFunction>) -> BcNativeFunction {
39-
let imp = fun.as_frozen_ref().map(|f| &*f.function);
40-
BcNativeFunction { fun, imp }
38+
BcNativeFunction {
39+
fun,
40+
imp: &fun.as_ref().function,
41+
}
4142
}
4243

4344
#[inline]

starlark/src/tests/bc/golden/call.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ Instructions:
2727
96: Const 50 ->&7
2828
120: CallFrozenNative noop {&3..&8 2 p q r *&0 **&1} instrs.star.bzl:2:5-10:6 ->&2
2929
# instrs.star.bzl:2:5-10:6
30-
208: ReturnConst None
31-
224: End
30+
200: ReturnConst None
31+
216: End

starlark/src/tests/bc/golden/def_inline_const_args_inlined.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ Instructions:
2020
24: Const 10 ->&2
2121
48: CallFrozenNativePos noop &1..&3 instrs.star.bzl:2:12-22 ->&0
2222
# instrs.star.bzl:6:5-25
23-
104: Return &0
24-
112: End
23+
96: Return &0
24+
104: End

starlark/src/tests/bc/golden/def_inline_locals_inlined.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ Instructions:
1818
# instrs.star.bzl:6:5-21
1919
0: CallFrozenNativePos noop &0..&2 instrs.star.bzl:2:12-22 ->&2
2020
# instrs.star.bzl:6:5-21
21-
56: Return &2
22-
64: End
21+
48: Return &2
22+
56: End

starlark/src/tests/bc/golden/definitely_assigned_mov_is_used.golden

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ def test(x, y): noop(y, x)
1010

1111
Max stack size: 3
1212
Instructions:
13-
# instrs.star.bzl:1:17-27
14-
0: Mov &y ->&3
15-
16: Mov &x ->&4
16-
32: CallFrozenNativePos noop &3..&5 instrs.star.bzl:1:17-27 ->&2
17-
# instrs.star.bzl:1:17-27
18-
88: ReturnConst None
19-
104: End
13+
# instrs.star.bzl:1:17-27
14+
0: Mov &y ->&3
15+
16: Mov &x ->&4
16+
32: CallFrozenNativePos noop &3..&5 instrs.star.bzl:1:17-27 ->&2
17+
# instrs.star.bzl:1:17-27
18+
80: ReturnConst None
19+
96: End

starlark/src/tests/bc/golden/definitely_assigned_slot_range_in_call.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ Instructions:
1313
# instrs.star.bzl:1:17-27
1414
0: CallFrozenNativePos noop &0..&2 instrs.star.bzl:1:17-27 ->&2
1515
# instrs.star.bzl:1:17-27
16-
56: ReturnConst None
17-
72: End
16+
48: ReturnConst None
17+
64: End

starlark/src/tests/bc/golden/expr_call_maybe_known_method.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ Instructions:
1414
0: Const 1 ->&2
1515
24: CallMaybeKnownMethodPos &x append <m> &2..&3 instrs.star.bzl:1:14-25 ->&1
1616
# instrs.star.bzl:1:14-25
17-
120: ReturnConst None
18-
136: End
17+
112: ReturnConst None
18+
128: End

starlark/src/tests/bc/golden/for.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def test(x):
1313
Max stack size: 2
1414
Instructions:
1515
# instrs.star.bzl:2:3-3:12
16-
0: Iter &x 0 ->&2 ->&i 104
16+
0: Iter &x 0 ->&2 ->&i 96
1717
# instrs.star.bzl:3:5-12
1818
> 24: CallFrozenNativePos noop &1..&2 instrs.star.bzl:3:5-12 ->&3
1919
# instrs.star.bzl:3:5-12
20-
80: Continue &2 0 ->&i 24 104
21-
>104: ReturnConst None
22-
120: End
20+
72: Continue &2 0 ->&i 24 96
21+
> 96: ReturnConst None
22+
112: End

0 commit comments

Comments
 (0)