-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
executeFile method - feature request #59
Comments
I'll take a look at this, but it makes sense to me. |
@cscott — What do you think about adding this feature? |
Sounds reasonable. It would be even more compelling if it was integrated
|
Thanks for your help and considering this request. The other advantage of having an executeFile method (and avoiding the extra call to file_get_contents) is that the raw JS source code doesn't need to be loaded into a PHP string, which may affect the net memory usage used by the script (with memory_get_usage). |
@cscott @preillyme do you know about the require function that is available from JS? This functionality could be re-used to provide module loading functionality which follows the CommonJS modules specification - see http://wiki.commonjs.org/wiki/Modules/1.1 Most of the groundwork had been done such as absolute / relative paths and reusing modules. |
wrt saving string storage space -- there's already a (good) suggestion in |
By the way I'm missing a function which can syntax check some javascript source in a string, i.e. compile and throw away afterwards. Maybe this could/should be considered if we decide to introduce the persistent compiled code thing. The function could be useful if you allow a user to modify some javascript code in the frontend and want to validate it within a save hook. |
There's already a 'require' method exported to JS. Presumably |
I tried running file:1: No module loader Does using the require method mean that the included file must be a javascript module? In my case, my included file contains a list of global functions like:
|
In order to use the JS require function, you need to use the PHP API to provide a module loader. Here's an example:
The point of this callback abstraction is security. I didn't want to give Javascript access to a function that can load and execute code from a file. Of course, loading and executing from a file may be a common use case of this anyway:
In practice you probably want to perform some sanity checking of the path to ensure that it's resolving to a file and that nothing malicious is going on. |
Also remember that the require'd files don't share the same global scope as your V8JS code. You need to use the "exports" variable to return an API to be used. See http://wiki.commonjs.org/wiki/Modules/1.1. Here's a really simple example of a module:
Then you require it in your code like this:
|
Thanks for your help. In my use case, I was just looking for a way to include a js file without having to load it first with file_get_contents - an executeFile method which is the exact equivalent of executeString except to pass it a file path. Similar to how other PHP extensions have functions to load either a string or a file - for example, DOMDocument::load and DOMDocument::loadXML or simplexml_load_file and simplexml_load_string. |
Revisiting this issue almost one year later ... and now having a After all we would have to provide Hence I'm now just closing this issue since there was no recent activity anyways. |
Thank you for a very useful extension. Would it be possible to add an executeFile() method that works just like executeString() except it takes a filename as its first argument? This would avoid having to call file_get_contents when the javascript to execute is in an external file.
For example, rather than doing:
$v8->executeString(file_get_contents('test.js'));
executeFile would allow calls like:
$v8->executeFile('test.js');
Thanks.
The text was updated successfully, but these errors were encountered: