facebook / starlark-rust

A Rust implementation of the Starlark language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Using `Dict` as a function return type with string values.

hulto opened this issue · comments

commented

This is dumb question but I'm having a hard time using the Dict or SmallMap<Value, Value> types as a return type for rust functions. Specifically when trying to return a Value holding a string.

  • I see in this issue the suggestion to use the evaluator heap. Is it recommended to pass a reference to the eval heap in each functions and perform allocations there?
  • Is there a better way in general to return a Map / dictionary from a starlark function?

Here's a test function i'm trying to add to the starlark library given a string, it should return a list of dictionary objects using Value<String> as the keys and values.
I'm able to use the frozen string value, and int but having issues with non-constant strings.
image

Thank you

You allocate a value in temporary heap, and then try to destroy in while returning a value owned by that heap. This is incorrect because memory would be freed when function returns.

If you need to return a heap allocated value, create a heap outside and pass a heap reference to the function.

Is there a better way in general to return a Map / dictionary from a starlark function?

Are you talking about native functions, generated with #[starlark_module]? It is not clear from your example.

commented

Thanks for the quick reply!

Are you talking about native functions, generated with #[starlark_module]? It is not clear from your example.

Yes sorry i am working on a function generated with #[starlark_module]

If you need to return a heap allocated value, create a heap outside and pass a heap reference to the function.

Makes sense. I tried to define a heap outside the function and pass a reference in. I ran into issues with what context the heap needs to live in.

Here's my first attempt seems like I need to have the heap defined outside this function. Makes sense.
image

After that I tried moving it outside to a higher context.
image

I experimented with some different closures but had a number of issues.
My understanding is that the #[starlark_module] macro is doing some rearranging and the defined functions are essentially anonymous and unable to inherit the context.

I looked at maybe passing the GlobalsBuilder heap in but I see that's a frozen heap.

How should I be passing the heap reference? Should I be using lifetimes to explicitly manage tmp_heap?

Updated function code to use heap reference.
image

How should I be passing the heap reference?

If this is a function inside #[starlark_module], like test_func_starlark in your example above, you just add &'v Heap parameter, and that's it.

Here is an example in repo test:

https://github.com/facebookexperimental/starlark-rust/blob/b139d571fa57d6a112c354da5890c55f57561435/starlark/src/tests/derive/module/special_params.rs#L26-L29

commented

That is incredibly cool. Thank you 🙂

The correct code looks like this if anyone else reads this.
image
image