Read the data value from a Function outside FunctionCallbackInfo?

44 views
Skip to first unread message

Al Mo

unread,
Apr 6, 2019, 7:43:31 PM4/6/19
to v8-users
I'm new here so first of all: Hi Everyone :D

So, let's say I make a function this way:

v8::Function::New(<Isolate>, <C_Function>, <Data_Value>);

I can then get the <Data_Value> when the Function is invoked with something like: FunctionCallbackInfo->GetData().

This is useful for many things. I'm looking for a way to be able to inspect this data value for a previously defined Function object. Something like Function->GetData(), you get the idea.

So far I haven't succeeded and I've read the docs from a to z. Any ideas?

Ben Noordhuis

unread,
Apr 8, 2019, 4:39:41 AM4/8/19
to v8-users
There is no such function or method at the moment. You could open a CL
(a code change) that adds it. See https://v8.dev/docs/contribute for
details on the process.

In case it helps, the path to get from a v8::Function to the `data`
property is this (I think):

1. Convert the v8::Function to a JSFunction
(`i::Handle<i::JSFunction>::cast(Utils::OpenHandle(this))` where
`this` is the v8::Function, see src/api.cc)

2. Get the SharedFunctionInfo, function->shared()

3. Get the FunctionTemplateInfo, shared->get_api_func_data() when
shared->IsApiFunction() is true

4. Get the CallHandlerInfo, function_template_info->call_code()

5. call_handler_info->data()

Hope that helps!

Al Mo

unread,
Apr 12, 2019, 10:27:17 AM4/12/19
to v8-users
I eventually solved it by binding a Private symbol with an external value to the Function object.

That solves it for the moment. I would love to contribute back to v8 but I'm very limited in time now. As soon as I find some time, I will submit a CL with an approach based on your suggestion.

Thanks.

Al Mo

unread,
Jun 4, 2020, 10:43:28 PM6/4/20
to v8-u...@googlegroups.com
[Back to this one year later :D]

I defined a new public member on v8::Function called 'GetData' that returns the data argument specified when the function was created. Seems to be working fine, here's the code in case someone wants to give it a quick glance:

v8::Local<v8::Value> Function::GetData() const {
  i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
  i::Isolate* isolate = self->GetIsolate();
  if(self->IsJSFunction()) {
    i::Handle<i::JSFunction> jsf = i::Handle<i::JSFunction>::cast(self);
    i::SharedFunctionInfo sfi = jsf->shared();
    if(sfi.IsApiFunction()) {
      i::FunctionTemplateInfo fti = sfi.get_api_func_data();
      i::CallHandlerInfo chi = i::CallHandlerInfo::cast(fti.call_code());
      i::Object o = chi.data();
      i::Handle<i::Object> ho = i::Handle<i::Object>(o, isolate);
      return ToApiHandle<v8::Value>(ho);
    }
  }
  return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
}


It is very verbose on purpose. I will submit a CL soon, I am reading the requirements :)

Best,
Alex.

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kosit la-orngsri

unread,
Aug 4, 2020, 9:32:04 PM8/4/20
to v8-users
สวัสดี

ในวันที่ วันศุกร์ที่ 5 มิถุนายน ค.ศ. 2020 เวลา 9 นาฬิกา 43 นาที 28 วินาที UTC+7 almo...@gmail.com เขียนว่า:
Reply all
Reply to author
Forward
0 new messages