Skip to content

Dev correct TSRMLS_CC #3

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
# Object files
*.o
*.ko
*.obj
*.elf

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
# Object files
*.o
*.ko
*.obj
*.elf
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
24 changes: 12 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
language: php
script: make test

php:
- 5.3
- 5.4
- 5.5

before_script:
- phpize
- ./configure --enable-phpjs
- make -j 4
language: php
script: make test
php:
- 5.3
- 5.4
- 5.5
before_script:
- phpize
- ./configure --enable-phpjs
- make -j 4
56 changes: 28 additions & 28 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
Copyright (c) 2015, César D. Rodas
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of PHPJS nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Copyright (c) 2015, César D. Rodas
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of PHPJS nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
233 changes: 178 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,178 @@
# PHPJS

***This is super experimental***

Run javascript inside PHP, powered by the awesome [Duktape](http://duktape.org) Javascript engine.

Why?
----

1. It's fun!
2. Javascript is becoming mainstream, with hundreds of libraries. Having an easy way of sharing code with Javascript natively makes using
3.

How to install it?
------------------

```bash
phpize
./configure --enable-js
make install
```

Then add `extension=phpjs.so` to your php.ini

How to use it?

There is `JS` class, each instance runs it's own Javascript *virtual machine* (or duktape's context).

```php
$js1 = new JS;
$js1->load("foobar.js");
```

The `JS` object is like a proxy between the `Javascript` and the `PHP` userlands. In the PHP side, the `JS` looks like an Array or an object.

```php
$js['foobar'] = 1; // Set `foobar` inside Javascript (global variable)
var_dump($js['XXX']); // Read global variable 'XXX' from javascript
$js->fnc(); // Call fnc() function from Javascript.
```

Inside `Javascript` there are also some intergration. For instance we have a `$PHP` (or PHP) global object.

```js
$PHP.var_dump("something"); // Call a PHP function!
$PHP.$something = 1; // set a variable
print($PHP.$something_else); // read a variable from PHP
```

TODO
----

1. Better sharing of objects between PHP and Javascript
2. More documentation
3. More integration :-)
# PHPJS

***This is experimental***

Run javascript inside PHP, powered by the awesome [Duktape](http://duktape.org) Javascript engine.

Why?
----

1. It's fun!
2. Javascript is becoming mainstream, with hundreds of libraries. Having an easy way of sharing code with Javascript natively makes using
3.

How to install it?
------------------

```bash
phpize
./configure --enable-phpjs
make install
```

Then add `extension=phpjs.so` to your php.ini

How to use it?

There is `JS` class, each instance runs it's own Javascript *virtual machine* (or duktape's context).


```php
$js1 = new JS;
$js1->load("foobar.js");
```

The `JS` object is like a proxy between the `Javascript` and the `PHP` userlands. In the PHP side, the `JS` looks like an Array or an object.

```php
$js['foobar'] = 1; // Set `foobar` inside Javascript (global variable)
var_dump($js['XXX']); // Read global variable 'XXX' from javascript
$js->fnc(); // Call fnc() function from Javascript.
```

Inside `Javascript` there are also some intergration. For instance we have a `PHP` (or `$PHP`) global object.

```js
PHP.var_dump("something"); // Call a PHP function!
PHP.$something = 1; // set a variable
print(PHP.$something_else); // read a variable from PHP
```

## Documentation

### JS Object (API)


#### __constructor

**@PARAMS**
$allowedVarAndFunc (Optional) : String[]
Array to PHP function and global variables updatable via PHP's object

```PHP
$VM = new JS(/*$allowedVarAndFunc*/);
// the VM can handle any PHP function and get/set any PHP global variables

$VM = new JS(['$myVar','$args','$argv','apache_getenv','apache_get_module','headers','date']);
// the VM can handle only PHP function and get/set any PHP global variables listed
```

#### load

**@PARAMS**
$filename : String
path to js source file (Absolute ou Relative)

```PHP
$VM = new JS;
$VM->load("file.js"); // relative path, throw an error if file not found
$VM->load("/tmp/file2.js"); // with an absolute path
```

#### evaluate

**@PARAMS**
$jsCode : String
path to js source to execute

```PHP
$VM = new JS;
$VM->evaluate("var i = 1; print('From JS CODE',i);");
$VM->evaluate("i++;print('From JS CODE',i);"); // allow to share data across code evalution (it's a same context)
```

### Modules

#### Module loading

PHPJS (Duktape) has a built-in minimal module loading framework based on [CommonJS modules version 1.1.1](http://wiki.commonjs.org/wiki/Modules/1.1.1), with additional support for module.exports.

Module is writen in javascript only

```JS
// Module foo/bar
exports.hello = function(){
print("world");
}
```

You can load modules from javascript code with the global require() function:

```JS
var mod = require('foo/bar');
mod.hello();
```

#### Module lookup function

The modules lookup function is the PHP's global `JSModSearch` or the local function `$JS->JSModSearch`

##### global lookup function

```PHP
// Global lookup function
function JSModSearch($id){
if ($id === 'foo') {
return 'exports.hello = function() { print("Hello from foo!"); };';
} else if ($id === 'bar') {
return 'exports.hello = function() { print("Hello from bar!"); };';
}
}
/* shared PHP vars and function */
$js = new JS([]);
$JS = '
var foo = require("foo");
foo.hello();
var bar = require("bar");
bar.hello();
try{
var nonExistentModule = require("nonExistentModule");
}catch(e){
print("expected exception: " + e.message);
}
';
$js->evaluate($JS);
```

##### local lookup function

```PHP
/* shared PHP vars and function */
$js = new JS();
// Local lookup function
$js->JSModSearch = function ($id){
if ($id === 'foo/bar') {
return 'exports.hello = function() { print("Hello from foo/bar!"); };';
} else if ($id === 'bar') {
return 'exports.hello = function() { print("Hello from bar!"); };';
}
};

$JS = '
var foo = require("foo");
foo.hello();
';
$js->evaluate($JS);
```

More
----

*For more documentation look tests folder*

TODO
----

1. Better sharing of objects between PHP and Javascript
2. More documentation
3. More integration :-)
12 changes: 6 additions & 6 deletions config.m4
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PHP_ARG_ENABLE(phpjs, [whether to enable phpjs support],
[ --enable-phpjs Enable phpjs support])

if test "$PHP_PHPJS" = "yes"; then
PHP_NEW_EXTENSION(phpjs, phpjs.c duktape.c interface.c functions.c objects.c wrapper.c,$ext_shared,,$P2C_CFLAGS)
fi
PHP_ARG_ENABLE(phpjs, [whether to enable phpjs support],
[ --enable-phpjs Enable phpjs support])
if test "$PHP_PHPJS" = "yes"; then
PHP_NEW_EXTENSION(phpjs, phpjs.c duktape.c interface.c functions.c objects.c wrapper.c,$ext_shared,,$P2C_CFLAGS)
fi
Loading