denoland / rusty_v8

Rust bindings for the V8 JavaScript engine
https://crates.io/crates/v8
MIT License
3.44k stars 317 forks source link

Get name of native code function #1346

Closed RoyalFoxy closed 5 months ago

RoyalFoxy commented 1 year ago

I'm currently facing a wall where I need to somehow get a single string into a native code function. Obviously I cannot just let the closure capture it as that would make the closure sized and that's a no go for how rusty_v8 is implemented.

I tried several methods ranging from trying a javascript child function which returns the callers name which works if the caller is a javascript function (tested in node.js) but not if the caller is a native code function which returns null.

// This is a (deprecated) function that would return the name of the parent function
function get_name() {
  const caller = arguments.callee.caller;
  return caller ? caller.name : null;
}

I also looked at the args.this() which I assume is the global context which matches up with javascript. I looked at the Function type in rusty_v8 and it seems to have getter and setter for it's name which is great! The problem here lies in the small detail that I have no clue how to get the actual function itself in the function. In javascript you can basically just reference the function like this

function return_myself() {
  return return_myself;
}

This works in javascript but you cannot do that in rust, not considering we're dealing with a closure here anyways...

I'd really like help on this manner as I'm lost and I can't find any documentation explaining how to get the name of the currently executed function.

devsnek commented 5 months ago

You can store data with the data function in FunctionBuilder.