Comments (13)
I'll take a look at this, but it makes sense to me.
from v8js.
@cscott — What do you think about adding this feature?
from v8js.
Sounds reasonable. It would be even more compelling if it was integrated
with the PHP autoloader so it could be smart-ish about paths and maybe
reuse loaded modules. Something like V8Js.require(filename). Because
otherwise it's just saving one method call to file_get_contents, which is a
little bit of convenience but maybe not enough to merit adding extra API.
On Oct 27, 2013 1:37 PM, "Patrick Reilly" [email protected] wrote:
@cscott https://github.com/cscott — What do you think about adding this
feature?—
Reply to this email directly or view it on GitHubhttps://github.com//issues/59#issuecomment-27174260
.
from v8js.
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).
from v8js.
@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.
from v8js.
wrt saving string storage space -- there's already a (good) suggestion in
the codebase to add a new API function to represent a compiled script.
This would be exported using the persistent PHP functionality, so that
scripts would only be loaded from string and compiled once, and then reused
by subsequent requests. This might also be a compelling reason/time to add
the 'load from file' argument, because we could just recheck the name of
the file (and the timestamp) before reusing the compiled string (instead of
having to load the entire string from the file just to check to see if it
was the same as the previously compiled source).
from v8js.
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.
from v8js.
There's already a 'require' method exported to JS. Presumably $v8->executeString('require("file.js")')
would do what the original requester wanted? If I go through with the refactoring discussed in issue #72, the require
method would be PHP-callable, so $v8->getUserContext()->require('file.js')
would work from PHP code.
from v8js.
I tried running $v8->executeString('require("file.js")')
, but I got back a V8JsScriptException:
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:
function test1() { ... }
function test2() { ... }
function test3() { ... }
from v8js.
In order to use the JS require function, you need to use the PHP API to provide a module loader. Here's an example:
$v8js->setModuleLoader(function($path) {
// Load from the given path and return JS code to be executed
});
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:
$c8js->setModuleLoader(function($path) {
return file_get_contents($path);
});
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.
from v8js.
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:
exports.add = function(a, b) {
return a + b;
};
exports.sub = function(a, b) {
return a - b;
};
Then you require it in your code like this:
var math = require("path/to/math");
var result = math.add(1, 1);
from v8js.
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.
from v8js.
Revisiting this issue almost one year later ... and now having a compileString
method as well ... I think we shouldn't add yet another method that just does the file_get_contents
and nothing more.
After all we would have to provide executeFile
as well as compileFile
... and don't know what not in the future.
Hence I'm now just closing this issue since there was no recent activity anyways.
from v8js.
Related Issues (20)
- Divergences & pull requests HOT 4
- Trying to compile with php8 branch on ubuntu 22, getting error: could not determine libv8 version (v8 compiled succesfully) HOT 3
- Dynamic Properties deprecation in PHP 8.2 HOT 4
- Broken build on PHP 8.0.x HOT 2
- Segmentation fault - possibly another GC issue? HOT 7
- Compromise between FORCE_ARRAY and standard setup HOT 8
- sandbox error with v8 11.1.277.13 HOT 1
- libv8 must be version 9.0 or greater (using dockerfile) HOT 1
- Issue running make in ubuntu 22.04 HOT 2
- windows php 8.1
- compile error with g++ 13: error: expected unqualified-id before '&' token
- With wich version of V8 this php library work ? HOT 4
- Error installing on debian 11
- This LIB is very hard to install ! HOT 14
- PROHIBITS_OVERWRITING removed from V8 and causing V8Js compile error. HOT 1
- V8Js 3.0.0 HOT 1
- Is the latest recommended release 2.1.1 or 2.1.2? HOT 4
- Do I have permission to refactor this? HOT 3
- Compilation error: "no matching function for call to 'v8::ObjectTemplate::SetAccessor" HOT 6
- Linux arm64 support? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from v8js.