-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
77 lines (66 loc) · 1.48 KB
/
main.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
#include <phpcpp.h>
#include <iostream>
extern "C" {
#include "duktape.h"
}
class Duktape : public Php::Base
{
private:
duk_context *ctx = NULL;
public:
Duktape() {
ctx = duk_create_heap_default();
}
virtual ~Duktape() {
duk_destroy_heap(ctx);
}
Php::Value eval(Php::Parameters ¶meters) {
const char *result;
if (parameters.size() != 1) {
throw Php::Exception("Error");
return "";
}
duk_push_string(ctx, parameters[0]);
int code = duk_peval(ctx);
if (code != DUK_ERR_NONE) {
switch (code)
{
case DUK_ERR_ERROR:
throw Php::Exception("Error");
break;
case DUK_ERR_EVAL_ERROR:
throw Php::Exception("EvalError");
break;
case DUK_ERR_RANGE_ERROR:
throw Php::Exception("EvalError");
break;
case DUK_ERR_REFERENCE_ERROR:
throw Php::Exception("ReferenceError");
break;
case DUK_ERR_SYNTAX_ERROR:
throw Php::Exception("SyntaxError");
break;
case DUK_ERR_TYPE_ERROR:
throw Php::Exception("TypeError");
break;
case DUK_ERR_URI_ERROR:
throw Php::Exception("URIError");
break;
default:
throw Php::Exception("Error");
break;
}
}
result = duk_safe_to_string(ctx, -1);
return result;
}
};
extern "C" {
PHPCPP_EXPORT void *get_module() {
static Php::Extension extension("duktape_php", "0.0.1");
Php::Class<Duktape> duktape("Duktape");
duktape.method("eval", &Duktape::eval);
extension.add(std::move(duktape));
return extension;
}
}