Git Product home page Git Product logo

php-v8's Introduction

php-v8

PHP extension for V8 JavaScript engine

Build Status GitHub license

This extension is for PHP 7 only.

This extension is still under heavy development and it public API may change without any warning. Use at your own risk.

About

php-v8 is a PHP 7.x extension that brings V8 JavaScript engine API to PHP with some abstraction in mind and provides an accurate native V8 C++ API implementation available from PHP.

Key features:

  • provides up-to-date JavaScript engine with recent ECMA features supported;
  • accurate native V8 C++ API implementation available from PHP;
  • solid experience between native V8 C++ API and V8 API in PHP;
  • no magic; no assumption;
  • does what it asked to do;
  • hides complexity with isolates and contexts scope management under the hood;
  • provides a both-way interaction with PHP and V8 objects, arrays and functions;
  • execution time and memory limits;
  • multiple isolates and contexts at the same time;
  • it works;

With this extension almost all that native V8 C++ API provides can be used. It provides a way to pass php scalars, objects and function to V8 runtime and specify interaction with passed values (objects and functions only, as scalars become js scalars too). While specific functionality will be done in PHP userland rather then in C/C++ this extension, it lets you get into V8 hacking faster, reduces time costs and gives you a more maintainable solution. And it doesn't make any assumptions for you, so you stay in control, it does exactly what you ask it to do.

With php-v8 you can even implement nodejs in PHP. Not sure whether anyone should/will do this anyway, but it's doable.

NOTE: Most, if not all, methods are named like in V8 API - starting from capital letter. This PSR violation done intentionally with the purpose to provide more solid experience between native V8 C++ API and V8 PHP API.

Demo

Here is a Hello World from V8 Getting Started developers guide page implemented in PHP with php-v8:

<?php
$isolate = new \V8\Isolate();
$context = new \V8\Context($isolate);
$source = new \V8\StringValue($isolate, "'Hello' + ', World!'");

$script = new \V8\Script($context, $source);
$result = $script->Run($context);

echo $result->ToString($context)->Value(), PHP_EOL;

which will output Hello, World!. See how it's shorter and readable from that C++ version? And it also doesn't limit you from V8 API utilizing to implement more amazing stuff.

Installation

Requirements

V8

You will need some fresh v8 Google JavaScript enging version installed. At this time extension tested on 5.4.420.

  • For Ubuntu there are pinepain/libv8-5.4 PPA. To install fresh libv8 do:

    $ sudo add-apt-repository -y ppa:pinepain/libv8-5.4
    $ sudo apt-get update -y
    $ sudo apt-get install -y libv8-5.4-dev
    
  • For OS X there are v8.rb homebrew formula. To install fresh libv8 do:

    $ brew install https://raw.githubusercontent.com/pinepain/php-v8/master/scripts/homebrew/v8.rb
    

PHP 7

  • For Ubuntu there are ondrej/php PPA by Ondřej Surý.

    To install fresh php7.0 do:

    $ sudo add-apt-repository -y ppa:ondrej/php
    $ sudo apt-get update -y
    $ sudo apt-get install -y php7.0
    
  • For OS X there are homebrew/homebrew-php tap with php70, php71 and large variety extensions for them.

    To install fresh php70 do:

    $ brew tap homebrew/homebrew-php
    $ brew install php70
    

Installing php-v8 from packages

  • For Ubuntu there are pinepain/php-v8 PPA.

    To install fresh php-v8:

    $ sudo add-apt-repository -y ppa:pinepain/php-v8
    $ sudo apt-get update -y
    $ sudo apt-get install -y php-v8
    
  • For OS X there are php70-v8.rb and php71-v8.rb homebrew formulas.

    To install fresh php70-v8 do:

    $ brew install https://raw.githubusercontent.com/pinepain/php-v8/master/scripts/homebrew/php70-v8.rb
    

Building php-v8 from sources

git clone https://github.com/pinepain/php-v8.git
cd php-v8
phpize && ./configure && make
make test

To install extension globally run

$ sudo make install

Developers note

  • to be able to customize some tests make sure you have variables_order = "EGPCS" in your php.ini

  • export DEV_TESTS=1 allows to run tests that made for development reason (e.g. test some weird behavior or for debugging)

  • To prevent asking test suite to send results to PHP QA team do export NO_INTERACTION=1

  • To track memory usage you may want to use smem, pmem and even lsof to see what shared object are loaded and free to display free and used memory in the system.

License

Copyright (c) 2015-2016 Bogdan Padalko <[email protected]>

php-v8 PHP extension is licensed under the MIT license.

php-v8's People

Contributors

pinepain avatar

Watchers

James Cloos avatar Pieter Hordijk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.