Re: [v8-users] Partially executing a Javascript file in v8 c++ program
Hi, function_value would be values that are passed to the function. function_name would be a v8::String which has the javascript function name function would be a v8::Function whcih is used to call the javascript function. This snippet would make it more clear auto js_strFunction = v8::String::NewFromUtf8(isolate,u8"main",v8::NewStringType::kNormal).ToLocalChecked(); v8::Local jsArgs; auto js_valFunction = context->Global()->Get(context,js_strFunction).ToLocalChecked(); auto js_Function = v8::Local::Cast(js_valFunction); auto js_MaybeValue = js_Function->Call(context,v8::Null(isolate),0,&jsArgs); Hope it's helpful. On Saturday, 5 August 2017 06:07:21 UTC+5:30, Kavin Mani wrote: > > This gave me a good head start. However, my program crashes at > function->Call statement. > > Can you please tell me what values function_value, function_name and > function are supposed to take.I have also populated argv with random values > since I am not using them in my function. If that could also be a reason > for the crash, please let me know. > > This is my JS code : > > Local source = > *String::NewFromUtf8(isolate, "'function test_function() {var match = 0; > return match+10;}'",* > * NewStringType::kNormal).ToLocalChecked();* > > *Thanks,* > *Kavin* > > On Friday, August 4, 2017 at 3:29:23 PM UTC-7, Jeremy Bettis wrote: >> >> What I do to call a function is to use >> >> v8::Local function_value; >> context->Global()->Get(context, function_name).ToLocal(&function_value); >> v8::Local function = >> v8::Local::Cast(function_value); >> const int argc = arguments.size(); >> std::vector> argv(argc); >> // populate the argv somehow >> function->Call(context, context->Global(), argc, argv.data()); >> >> On Fri, Aug 4, 2017 at 4:17 PM, Kavin Mani wrote: >> >>> Hi, >>> >>> I am new to V8 engine and starting to experiment things. I am wondering >>> if it is possible to execute different functions from a Javascript file at >>> different times. >>> >>> Consider the following scenario: >>> >>> I have a Javascript file with a global variable *foo *and two functions >>> *func1() *and *func2().* Something like this: >>> >>> *Test.js:* >>> >>> var foo = 0; >>> >>> function func1() { >>> foo = 100; >>> } >>> >>> function func2() { >>> return foo + 10; >>> } >>> >>> In my *cpp* file, I want to first execute *func1() *from Test.js*, *print >>> some random statements (need not be related to using *foo*) and then >>> call *func2()*. Finally, I want to print the value of *foo *in my C++ >>> program. Here is what I want to do - >>> >>> *Test.cpp* >>> >>> Print some statements... >>> >>> Call func1() >>> >>> Print some more statements... >>> >>> Call func2() >>> >>> Print the value of foo >>> >>> >>> Almost all of the examples that I could find takes the Javascript >>> program as a string and executes it completely. However, I want to execute >>> them in parts at different times. Can someone please help me to find a way >>> to do this? >>> >>> Thanks, >>> Kavin >>> >>> -- >>> -- >>> 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. >>> >> >> >> >> -- >> Jeremy Bettis | Staff Software Engineer | jbe...@google.com | >> 303-257-2486 >> > -- -- v8-users mailing list v8-users@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [v8-users] Partially executing a Javascript file in v8 c++ program
This gave me a good head start. However, my program crashes at function->Call statement. Can you please tell me what values function_value, function_name and function are supposed to take.I have also populated argv with random values since I am not using them in my function. If that could also be a reason for the crash, please let me know. This is my JS code : Local source = *String::NewFromUtf8(isolate, "'function test_function() {var match = 0; return match+10;}'",* * NewStringType::kNormal).ToLocalChecked();* *Thanks,* *Kavin* On Friday, August 4, 2017 at 3:29:23 PM UTC-7, Jeremy Bettis wrote: > > What I do to call a function is to use > > v8::Local function_value; > context->Global()->Get(context, function_name).ToLocal(&function_value); > v8::Local function = > v8::Local::Cast(function_value); > const int argc = arguments.size(); > std::vector> argv(argc); > // populate the argv somehow > function->Call(context, context->Global(), argc, argv.data()); > > On Fri, Aug 4, 2017 at 4:17 PM, Kavin Mani > wrote: > >> Hi, >> >> I am new to V8 engine and starting to experiment things. I am wondering >> if it is possible to execute different functions from a Javascript file at >> different times. >> >> Consider the following scenario: >> >> I have a Javascript file with a global variable *foo *and two functions >> *func1() *and *func2().* Something like this: >> >> *Test.js:* >> >> var foo = 0; >> >> function func1() { >> foo = 100; >> } >> >> function func2() { >> return foo + 10; >> } >> >> In my *cpp* file, I want to first execute *func1() *from Test.js*, *print >> some random statements (need not be related to using *foo*) and then >> call *func2()*. Finally, I want to print the value of *foo *in my C++ >> program. Here is what I want to do - >> >> *Test.cpp* >> >> Print some statements... >> >> Call func1() >> >> Print some more statements... >> >> Call func2() >> >> Print the value of foo >> >> >> Almost all of the examples that I could find takes the Javascript program >> as a string and executes it completely. However, I want to execute them in >> parts at different times. Can someone please help me to find a way to do >> this? >> >> Thanks, >> Kavin >> >> -- >> -- >> 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. >> > > > > -- > Jeremy Bettis | Staff Software Engineer | jbe...@google.com > | 303-257-2486 > -- -- v8-users mailing list v8-users@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [v8-users] Partially executing a Javascript file in v8 c++ program
What I do to call a function is to use v8::Local function_value; context->Global()->Get(context, function_name).ToLocal(&function_value); v8::Local function = v8::Local::Cast(function_value); const int argc = arguments.size(); std::vector> argv(argc); // populate the argv somehow function->Call(context, context->Global(), argc, argv.data()); On Fri, Aug 4, 2017 at 4:17 PM, Kavin Mani wrote: > Hi, > > I am new to V8 engine and starting to experiment things. I am wondering if > it is possible to execute different functions from a Javascript file at > different times. > > Consider the following scenario: > > I have a Javascript file with a global variable *foo *and two functions > *func1() *and *func2().* Something like this: > > *Test.js:* > > var foo = 0; > > function func1() { > foo = 100; > } > > function func2() { > return foo + 10; > } > > In my *cpp* file, I want to first execute *func1() *from Test.js*, *print > some random statements (need not be related to using *foo*) and then call > *func2()*. Finally, I want to print the value of *foo *in my C++ program. > Here is what I want to do - > > *Test.cpp* > > Print some statements... > > Call func1() > > Print some more statements... > > Call func2() > > Print the value of foo > > > Almost all of the examples that I could find takes the Javascript program > as a string and executes it completely. However, I want to execute them in > parts at different times. Can someone please help me to find a way to do > this? > > Thanks, > Kavin > > -- > -- > v8-users mailing list > v8-users@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+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- Jeremy Bettis | Staff Software Engineer | jbet...@google.com | 303-257-2486 -- -- v8-users mailing list v8-users@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout. smime.p7s Description: S/MIME Cryptographic Signature