-
Notifications
You must be signed in to change notification settings - Fork 25
/
extension.cpp
91 lines (77 loc) · 2.86 KB
/
extension.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* extension.cpp
*
* Startup file for the PHP extension
*
* @copyright 2015 Copernica BV
*/
/**
* Dependencies
*/
#include <phpcpp.h>
#include "context.h"
#include "jsobject.h"
#include "isolate.h"
#include "platform.h"
#include <iostream>
/**
* The VERSION macro is going to be used as string with surrounded quotes
*/
#define STR_VALUE(arg) #arg
#define VERSION_NAME(name) STR_VALUE(name)
#define THE_VERSION VERSION_NAME(VERSION)
/**
* tell the compiler that the get_module is a pure C function
*/
extern "C" {
/**
* Function that is called by PHP right after the PHP process
* has started, and that returns an address of an internal PHP
* strucure with all the details and features of your extension
*
* @return void* a pointer to an address that is understood by PHP
*/
PHPCPP_EXPORT void *get_module()
{
// static(!) Php::Extension object that should stay in memory
// for the entire duration of the process (that's why it's static)
static Php::Extension extension("PHP-JS", THE_VERSION);
// declare the accessor attributes
extension.add(Php::Constant("JS\\None", v8::None));
extension.add(Php::Constant("JS\\ReadOnly", v8::ReadOnly));
extension.add(Php::Constant("JS\\DontDelete", v8::DontDelete));
extension.add(Php::Constant("JS\\DontEnumerate",v8::DontEnum));
// create our context class
Php::Class<JS::Context> context("JS\\Context");
// properties can be assigned
context.method<&JS::Context::assign>("assign", {
Php::ByVal("name", Php::Type::String, true),
Php::ByVal("value", Php::Type::Null, true),
Php::ByVal("attribute", Php::Type::Numeric, false)
});
// add a method to execute some script
context.method<&JS::Context::evaluate>("evaluate", {
Php::ByVal("script", Php::Type::String, true),
Php::ByVal("timeout", Php::Type::Numeric, false)
});
// an empty class for exporting object from ecmascript
Php::Class<JS::JSObject> object("JS\\Object");
// add the classes to the extension
extension.add(std::move(context));
extension.add(std::move(object));
// the isolate should get cleaned up after every pageview
// but because php can still keep references to c++ objects
// alive until after onIdle we clean up before every pageview
// and on shutdown instead
extension.onRequest(JS::Isolate::destroy);
// the platform and isolate needs to be cleaned up on engine shutdown
extension.onShutdown([]{
// clean up the isolate
JS::Isolate::destroy();
// clean up the platform
JS::Platform::shutdown();
});
// return the extension
return extension;
}
}