summaryrefslogtreecommitdiff
path: root/pdf/pdf_jsimp_v8.cpp
blob: 837a4919022512dcb6eadeb0ac2e6258e5020d1c (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * This is a dummy JavaScript engine. It cheats by recognising the specific
 * strings in calc.pdf, and hence will work only for that file. It is for
 * testing only.
 */

extern "C" {
#include "fitz-internal.h"
#include "mupdf-internal.h"
}

#include <vector>
#include <set>
#include <v8.h>

using namespace v8;
using namespace std;

/* Object we pass to FunctionTemplate::New, which v8 passes back to us in
 * callMethod, allowing us to call our client's, passed-in method. */
struct PDFJSImpMethod
{
	void             *jsctx;
	pdf_jsimp_method *meth;

	PDFJSImpMethod(void *jsctx, pdf_jsimp_method *meth) : jsctx(jsctx), meth(meth) {}
};

/* Object we pass to ObjectTemplate::SetAccessor, which v8 passes back to us in
 * setProp and getProp, allowing us to call our client's, passed-in set/get methods. */
struct PDFJSImpProperty
{
	void             *jsctx;
	pdf_jsimp_getter *get;
	pdf_jsimp_setter *set;

	PDFJSImpProperty(void *jsctx, pdf_jsimp_getter *get, pdf_jsimp_setter *set) : jsctx(jsctx), get(get), set(set) {}
};

struct PDFJSImp;

/* Internal representation of the pdf_jsimp_type object */
struct PDFJSImpType
{
	PDFJSImp                  *imp;
	Persistent<ObjectTemplate> templ;
	pdf_jsimp_dtr             *dtr;
	vector<PDFJSImpMethod *> methods;
	vector<PDFJSImpProperty *> properties;

	PDFJSImpType(PDFJSImp *imp, pdf_jsimp_dtr *dtr): imp(imp), dtr(dtr)
	{
		HandleScope scope;
		templ = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
		templ->SetInternalFieldCount(1);
	}

	~PDFJSImpType()
	{
		vector<PDFJSImpMethod *>::iterator mit;
		for (mit = methods.begin(); mit < methods.end(); mit++)
			delete *mit;

		vector<PDFJSImpProperty *>::iterator pit;
		for (pit = properties.begin(); pit < properties.end(); pit++)
			delete *pit;

		templ.Dispose();
	}
};

/* Info via which we destroy the client side part of objects that
 * v8 garbage collects */
struct PDFJSImpGCObj
{
	Persistent<Object> pobj;
	PDFJSImpType *type;

	PDFJSImpGCObj(Handle<Object> obj, PDFJSImpType *type): type(type)
	{
		pobj = Persistent<Object>::New(obj);
	}

	~PDFJSImpGCObj()
	{
		pobj.Dispose();
	}
};

/* Internal representation of the pdf_jsimp object */
struct PDFJSImp
{
	fz_context			*ctx;
	void				*jsctx;
	Persistent<Context>	 context;
	vector<PDFJSImpType *> types;
	set<PDFJSImpGCObj *> gclist;

	PDFJSImp(fz_context *ctx, void *jsctx) : ctx(ctx), jsctx(jsctx)
	{
		HandleScope scope;
		context = Persistent<Context>::New(Context::New());
	}

	~PDFJSImp()
	{
		HandleScope scope;
		/* Tell v8 our context will not be used again */
		context.Dispose();

		/* Unlink and destroy all the objects that v8 has yet to gc */
		set<PDFJSImpGCObj *>::iterator oit;
		for (oit = gclist.begin(); oit != gclist.end(); oit++)
		{
			(*oit)->pobj.ClearWeak(); /* So that gcCallback wont get called */
			PDFJSImpType *vType = (*oit)->type;
			Local<External> owrap = Local<External>::Cast((*oit)->pobj->GetInternalField(0));
			vType->dtr(vType->imp->jsctx, owrap->Value());
			delete *oit;
		}

		vector<PDFJSImpType *>::iterator it;
		for (it = types.begin(); it < types.end(); it++)
			delete *it;
	}
};

/* Internal representation of the pdf_jsimp_obj object */
class PDFJSImpObject
{
	Persistent<Value>   pobj;
	String::Utf8Value  *utf8;

public:
	PDFJSImpObject(Handle<Value> obj): utf8(NULL)
	{
		pobj = Persistent<Value>::New(obj);
	}

	PDFJSImpObject(const char *str): utf8(NULL)
	{
		pobj = Persistent<Value>::New(String::New(str));
	}

	~PDFJSImpObject()
	{
		delete utf8;
		pobj.Dispose();
	}

	char *toString()
	{
		delete utf8;
		utf8 = new String::Utf8Value(pobj);
		return **utf8;
	}

	Handle<Value> toValue()
	{
		return pobj;
	}
};


extern "C" pdf_jsimp *pdf_new_jsimp(fz_context *ctx, void *jsctx)
{
	return reinterpret_cast<pdf_jsimp *>(new PDFJSImp(ctx, jsctx));
}

extern "C" void pdf_drop_jsimp(pdf_jsimp *imp)
{
	delete reinterpret_cast<PDFJSImp *>(imp);
}

extern "C" pdf_jsimp_type *pdf_jsimp_new_type(pdf_jsimp *imp, pdf_jsimp_dtr *dtr)
{
	PDFJSImp *vImp = reinterpret_cast<PDFJSImp *>(imp);
	PDFJSImpType *vType = new PDFJSImpType(vImp, dtr);
	vImp->types.push_back(vType);
	return reinterpret_cast<pdf_jsimp_type *>(vType);
}

extern "C" void pdf_jsimp_drop_type(pdf_jsimp *imp, pdf_jsimp_type *type)
{
	/* Types are recorded and destroyed as part of PDFJSImp */
}

static Handle<Value> callMethod(const Arguments &args)
{
	HandleScope scope;
	Local<External> mwrap = Local<External>::Cast(args.Data());
	PDFJSImpMethod *m = (PDFJSImpMethod *)mwrap->Value();

	Local<Object> self = args.Holder();
	Local<External> owrap;
	void *nself = NULL;
	if (self->InternalFieldCount() > 0)
	{
		owrap = Local<External>::Cast(self->GetInternalField(0));
		nself = owrap->Value();
	}

	int c = args.Length();
	PDFJSImpObject **native_args = new PDFJSImpObject*[c];
	for (int i = 0; i < c; i++)
		native_args[i] = new PDFJSImpObject(args[i]);

	PDFJSImpObject *obj = reinterpret_cast<PDFJSImpObject *>(m->meth(m->jsctx, nself, c, reinterpret_cast<pdf_jsimp_obj **>(native_args)));
	Handle<Value> val = obj->toValue();
	delete obj;

	for (int i = 0; i < c; i++)
		delete native_args[i];

	delete native_args;

	return scope.Close(val);
}

extern "C" void pdf_jsimp_addmethod(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_method *meth)
{
	PDFJSImpType *vType = reinterpret_cast<PDFJSImpType *>(type);
	HandleScope scope;

	PDFJSImpMethod *pmeth = new PDFJSImpMethod(vType->imp->jsctx, meth);
	vType->templ->Set(String::New(name), FunctionTemplate::New(callMethod, External::New(pmeth)));
	vType->methods.push_back(pmeth);
}

static Handle<Value> getProp(Local<String> property, const AccessorInfo &info)
{
	HandleScope scope;
	Local<External> pwrap = Local<External>::Cast(info.Data());
	PDFJSImpProperty *p = reinterpret_cast<PDFJSImpProperty *>(pwrap->Value());

	Local<Object> self = info.Holder();
	Local<External> owrap;
	void *nself = NULL;
	if (self->InternalFieldCount() > 0)
	{
		owrap = Local<External>::Cast(self->GetInternalField(0));
		nself = owrap->Value();
	}

	PDFJSImpObject *obj = reinterpret_cast<PDFJSImpObject *>(p->get(p->jsctx, nself));
	Handle<Value> val = obj->toValue();
	delete obj;
	return scope.Close(val);
}

static void setProp(Local<String> property, Local<Value> value, const AccessorInfo &info)
{
	HandleScope scope;
	Local<External> wrap = Local<External>::Cast(info.Data());
	PDFJSImpProperty *p = reinterpret_cast<PDFJSImpProperty *>(wrap->Value());

	Local<Object> self = info.Holder();
	Local<External> owrap;
	void *nself = NULL;
	if (self->InternalFieldCount() > 0)
	{
		owrap = Local<External>::Cast(self->GetInternalField(0));
		nself = owrap->Value();
	}

	PDFJSImpObject *obj = new PDFJSImpObject(value);

	p->set(p->jsctx, nself, reinterpret_cast<pdf_jsimp_obj *>(obj));
	delete obj;
}

extern "C" void pdf_jsimp_addproperty(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_getter *get, pdf_jsimp_setter *set)
{
	PDFJSImpType *vType = reinterpret_cast<PDFJSImpType *>(type);
	HandleScope scope;

	PDFJSImpProperty *prop = new PDFJSImpProperty(vType->imp->jsctx, get, set);
	vType->templ->SetAccessor(String::New(name), getProp, setProp, External::New(prop));
	vType->properties.push_back(prop);
}

extern "C" void pdf_jsimp_set_global_type(pdf_jsimp *imp, pdf_jsimp_type *type)
{
	PDFJSImp	 *vImp  = reinterpret_cast<PDFJSImp *>(imp);
	PDFJSImpType *vType = reinterpret_cast<PDFJSImpType *>(type);
	HandleScope scope;

	vImp->context = Persistent<Context>::New(Context::New(NULL, vType->templ));
}

static void gcCallback(Persistent<Value> val, void *parm)
{
	PDFJSImpGCObj *gco = reinterpret_cast<PDFJSImpGCObj *>(parm);
	PDFJSImpType *vType = gco->type;
	HandleScope scope;
	Persistent<Object> obj = Persistent<Object>::Cast(val);

	Local<External> owrap = Local<External>::Cast(obj->GetInternalField(0));
	vType->dtr(vType->imp->jsctx, owrap->Value());
	vType->imp->gclist.erase(gco);
	delete gco; /* Disposes of the persistent handle */
}

extern "C" pdf_jsimp_obj *pdf_jsimp_new_obj(pdf_jsimp *imp, pdf_jsimp_type *type, void *natobj)
{
	PDFJSImpType *vType = reinterpret_cast<PDFJSImpType *>(type);
	HandleScope scope;
	Local<Object> obj = vType->templ->NewInstance();
	obj->SetInternalField(0, External::New(natobj));

	/* Arrange for destructor to be called on the client sire object
	 * when the v8 object is garbage collected */
	if (vType->dtr)
	{
		PDFJSImpGCObj *gco = new PDFJSImpGCObj(obj, vType);
		vType->imp->gclist.insert(gco);
		gco->pobj.MakeWeak(gco, gcCallback);
	}

	return reinterpret_cast<pdf_jsimp_obj *>(new PDFJSImpObject(obj));
}

extern "C" void pdf_jsimp_drop_obj(pdf_jsimp *imp, pdf_jsimp_obj *obj)
{
	delete reinterpret_cast<PDFJSImpObject *>(obj);
}

extern "C" pdf_jsimp_obj *pdf_jsimp_fromString(pdf_jsimp *imp, char *str)
{
	return reinterpret_cast<pdf_jsimp_obj *>(new PDFJSImpObject(str));
}

extern "C" char *pdf_jsimp_toString(pdf_jsimp *imp, pdf_jsimp_obj *obj)
{
	return reinterpret_cast<PDFJSImpObject *>(obj)->toString();
}

extern "C" void pdf_jsimp_execute(pdf_jsimp *imp, char *code)
{
	PDFJSImp *vImp = reinterpret_cast<PDFJSImp *>(imp);
	HandleScope scope;
	Context::Scope context_scope(vImp->context);
	Script::Compile(String::New(code))->Run();
}

extern "C" void pdf_jsimp_execute_count(pdf_jsimp *imp, char *code, int count)
{
	PDFJSImp *vImp = reinterpret_cast<PDFJSImp *>(imp);
	HandleScope scope;
	Context::Scope context_scope(vImp->context);
	Script::Compile(String::New(code, count))->Run();
}