Skip to content

Commit a92a604

Browse files
Added initial implementation
1 parent 618065c commit a92a604

17 files changed

+1476
-0
lines changed

Makefile

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#
2+
# Makefile template
3+
#
4+
# This is an example Makefile that can be used by anyone who is building
5+
# his or her own PHP extensions using the PHP-CPP library.
6+
#
7+
# In the top part of this file we have included variables that can be
8+
# altered to fit your configuration, near the bottom the instructions and
9+
# dependencies for the compiler are defined. The deeper you get into this
10+
# file, the less likely it is that you will have to change anything in it.
11+
#
12+
13+
#
14+
# Name of your extension
15+
#
16+
# This is the name of your extension. Based on this extension name, the
17+
# name of the library file (name.so) and the name of the config file (name.ini)
18+
# are automatically generated
19+
#
20+
21+
NAME = pxjavascript
22+
23+
24+
#
25+
# Php.ini directories
26+
#
27+
# In the past, PHP used a single php.ini configuration file. Today, most
28+
# PHP installations use a conf.d directory that holds a set of config files,
29+
# one for each extension. Use this variable to specify this directory.
30+
#
31+
32+
INI_DIR = /etc/php5/mods-available/
33+
34+
35+
#
36+
# The extension dirs
37+
#
38+
# This is normally a directory like /usr/lib/php5/20121221 (based on the
39+
# PHP version that you use. We make use of the command line 'php-config'
40+
# instruction to find out what the extension directory is, you can override
41+
# this with a different fixed directory
42+
#
43+
44+
EXTENSION_DIR = $(shell php-config --extension-dir)
45+
46+
47+
#
48+
# The name of the extension and the name of the .ini file
49+
#
50+
# These two variables are based on the name of the extension. We simply add
51+
# a certain extension to them (.so or .ini)
52+
#
53+
54+
EXTENSION = ${NAME}.so
55+
INI = ${NAME}.ini
56+
57+
58+
#
59+
# Compiler
60+
#
61+
# By default, the GNU C++ compiler is used. If you want to use a different
62+
# compiler, you can change that here. You can change this for both the
63+
# compiler (the program that turns the c++ files into object files) and for
64+
# the linker (the program that links all object files into the single .so
65+
# library file. By default, g++ (the GNU C++ compiler) is used for both.
66+
#
67+
68+
COMPILER = c++
69+
LINKER = c++
70+
71+
72+
#
73+
# Compiler and linker flags
74+
#
75+
# This variable holds the flags that are passed to the compiler. By default,
76+
# we include the -O2 flag. This flag tells the compiler to optimize the code,
77+
# but it makes debugging more difficult. So if you're debugging your application,
78+
# you probably want to remove this -O2 flag. At the same time, you can then
79+
# add the -g flag to instruct the compiler to include debug information in
80+
# the library (but this will make the final libphpcpp.so file much bigger, so
81+
# you want to leave that flag out on production servers).
82+
#
83+
# If your extension depends on other libraries (and it does at least depend on
84+
# one: the PHP-CPP library), you should update the LINKER_DEPENDENCIES variable
85+
# with a list of all flags that should be passed to the linker.
86+
#
87+
88+
COMPILER_FLAGS = -Wall -c -O2 -std=c++11 -fpic -DVERSION="`./version.sh`" -I. -g
89+
LINKER_FLAGS = -shared
90+
LINKER_DEPENDENCIES = -lphpcpp -lv8
91+
92+
93+
#
94+
# Command to remove files, copy files and create directories.
95+
#
96+
# I've never encountered a *nix environment in which these commands do not work.
97+
# So you can probably leave this as it is
98+
#
99+
100+
RM = rm -f
101+
CP = cp -f
102+
MKDIR = mkdir -p
103+
104+
105+
#
106+
# All source files are simply all *.cpp files found in the current directory
107+
#
108+
# A builtin Makefile macro is used to scan the current directory and find
109+
# all source files. The object files are all compiled versions of the source
110+
# file, with the .cpp extension being replaced by .o.
111+
#
112+
113+
SOURCES = $(wildcard *.cpp)
114+
OBJECTS = $(SOURCES:%.cpp=%.o)
115+
116+
117+
#
118+
# From here the build instructions start
119+
#
120+
121+
all: ${OBJECTS} ${EXTENSION}
122+
123+
${EXTENSION}: ${OBJECTS}
124+
${LINKER} ${LINKER_FLAGS} -o $@ ${OBJECTS} ${LINKER_DEPENDENCIES}
125+
126+
${OBJECTS}:
127+
${COMPILER} ${COMPILER_FLAGS} -o $@ ${@:%.o=%.cpp}
128+
129+
install:
130+
${CP} ${EXTENSION} ${EXTENSION_DIR}
131+
${CP} ${INI} ${INI_DIR}
132+
133+
clean:
134+
${RM} ${EXTENSION} ${OBJECTS}
135+

context.cpp

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* context.h
3+
*
4+
* The main javascript class, used for assigning variables
5+
* and executing javascript
6+
*
7+
* @copyright 2015 Copernica B.V.
8+
*/
9+
10+
/**
11+
* Dependencies
12+
*/
13+
#include "context.h"
14+
#include "isolate.h"
15+
#include "value.h"
16+
17+
/**
18+
* Start namespace
19+
*/
20+
namespace JS {
21+
22+
/**
23+
* Constructor
24+
*/
25+
Context::Context()
26+
{
27+
// temporary handle, necessary to store the values before the are put in the unique persistent handle
28+
// note, since we want this variable to fall out of scope, we cannot use member initialization
29+
v8::HandleScope scope(isolate());
30+
31+
// now create the context
32+
_context = v8::Context::New(isolate(), nullptr);
33+
}
34+
35+
36+
/**
37+
* Assign a variable to the javascript context
38+
*
39+
* @param params array of parameters:
40+
* - string name of property to assign required
41+
* - mixed property value to assign required
42+
* - integer property attributes optional
43+
*
44+
* The property attributes can be one of the following values
45+
*
46+
* - ReadOnly
47+
* - DontEnum
48+
* - DontDelete
49+
*
50+
* If not specified, the property will be writable, enumerable and
51+
* deletable.
52+
*/
53+
void Context::assign(Php::Parameters &params)
54+
{
55+
// create a local "scope" and "enter" our context
56+
v8::HandleScope scope(isolate());
57+
v8::Context::Scope contextScope(_context);
58+
59+
// retrieve the global object from the context
60+
v8::Local<v8::Object> global(_context->Global());
61+
62+
// the attribute for the newly assigned property
63+
v8::PropertyAttribute attribute(v8::None);
64+
65+
// if an attribute was given, assign it
66+
if (params.size() > 2)
67+
{
68+
// check the attribute that was selected
69+
switch ((int16_t)params[2])
70+
{
71+
case v8::None: attribute = v8::None; break;
72+
case v8::ReadOnly: attribute = v8::ReadOnly; break;
73+
case v8::DontDelete: attribute = v8::DontDelete; break;
74+
case v8::DontEnum: attribute = v8::DontEnum; break;
75+
}
76+
}
77+
78+
// and store the value
79+
global->ForceSet(value(params[0]), value(params[1]), attribute);
80+
}
81+
82+
/**
83+
* Parse a piece of javascript code
84+
*
85+
* @param params array with one parameter: the code to execute
86+
* @return Php::Value
87+
*/
88+
Php::Value Context::evaluate(Php::Parameters &params)
89+
{
90+
// create a handle, so that all variables fall "out of scope"
91+
v8::HandleScope scope(isolate());
92+
93+
// enter the compilation/execution scope
94+
v8::Context::Scope contextScope(_context);
95+
96+
// catch any errors that occur while either compiling or running the script
97+
v8::TryCatch catcher;
98+
99+
// compile the code into a script
100+
v8::Local<v8::String> source(v8::String::NewFromUtf8(isolate(), params[0]));
101+
v8::Local<v8::Script> script(v8::Script::Compile(source));
102+
103+
// execute the script
104+
v8::Local<v8::Value> result(script->Run());
105+
106+
// did we catch an exception?
107+
if (catcher.HasCaught())
108+
{
109+
// retrieve the message describing the problem
110+
v8::Local<v8::Message> message(catcher.Message());
111+
v8::Local<v8::String> description(message->Get());
112+
113+
// convert the description to utf so we can dump it
114+
v8::String::Utf8Value string(description);
115+
116+
// pass this exception on to PHP userspace
117+
throw Php::Exception(std::string(*string, string.length()));
118+
}
119+
120+
// return the result
121+
return value(result);
122+
}
123+
124+
/**
125+
* End namespace
126+
*/
127+
}

context.h

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* context.h
3+
*
4+
* The main javascript class, used for assigning variables
5+
* and executing javascript
6+
*
7+
* @copyright 2015 Copernica B.V.
8+
*/
9+
10+
/**
11+
* Include guard
12+
*/
13+
#pragma once
14+
15+
/**
16+
* Dependencies
17+
*/
18+
#include <phpcpp.h>
19+
#include <v8.h>
20+
#include "stack.h"
21+
22+
/**
23+
* Start namespace
24+
*/
25+
namespace JS {
26+
27+
/**
28+
* Class definition
29+
*/
30+
class Context : public Php::Base
31+
{
32+
private:
33+
/**
34+
* The context
35+
* @var Stack<V8::Context>
36+
*/
37+
Stack<v8::Context> _context;
38+
public:
39+
/**
40+
* Constructor
41+
*/
42+
Context();
43+
44+
/**
45+
* No copying allowed
46+
*
47+
* @param that the object we cannot copy
48+
*/
49+
Context(const Context &that) = delete;
50+
51+
/**
52+
* Move constructor
53+
*
54+
* @param that object to move
55+
*/
56+
Context(Context&&that) = default;
57+
58+
/**
59+
* Assign a variable to the javascript context
60+
*
61+
* @param params array of parameters:
62+
* - string name of property to assign required
63+
* - mixed property value to assign required
64+
* - integer property attributes optional
65+
*
66+
* The property attributes can be one of the following values
67+
*
68+
* - ReadOnly
69+
* - DontEnum
70+
* - DontDelete
71+
*
72+
* If not specified, the property will be writable, enumerable and
73+
* deletable.
74+
*/
75+
void assign(Php::Parameters &params);
76+
77+
/**
78+
* Parse a piece of javascript code
79+
*
80+
* @param params array with one parameter: the code to execute
81+
* @return Php::Value
82+
*/
83+
Php::Value evaluate(Php::Parameters &params);
84+
};
85+
86+
/**
87+
* End namespace
88+
*/
89+
}

0 commit comments

Comments
 (0)