summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/ecmascript/v8/V8DOM.h
blob: 43b98cea1f9873458c828fd931c624ef90ac184f (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
#ifndef V8DOM_H_LKE1HKJK
#define V8DOM_H_LKE1HKJK

#include "uscxml/Interpreter.h"
#include <v8.h>
#include <XPath/XPath.hpp>
#include "../Storage.h"

#define V8_DESTRUCTOR(type) \
static void jsDestructor(v8::Persistent<v8::Value> object, void* data) { \
  v8::HandleScope handleScope; \
  type* thing = static_cast<type*>(v8::Local<v8::External>::Cast(object->ToObject()->GetInternalField(0))->Value()); \
  delete thing->nativeObj; \
  delete thing; \
  object.Dispose(); \
  object.Clear(); \
}

#define V8_DESTRUCTOR_KEEP_WRAPPED(type) \
static void jsDestructor(v8::Persistent<v8::Value> object, void* data) { \
v8::HandleScope handleScope; \
type* thing = static_cast<type*>(v8::Local<v8::External>::Cast(object->ToObject()->GetInternalField(0))->Value()); \
delete thing; \
object.Dispose(); \
object.Clear(); \
}

namespace Arabica {
namespace DOM {

class V8DOM {
public:
	V8DOM();
	virtual ~V8DOM();

	template <typename T>
	static T* toClassPtr(v8::Local<v8::Value> data) {
		if(data.IsEmpty())
			return NULL;
		else if(!data->IsExternal())
			return NULL;
		else
			return static_cast<T *>(v8::External::Unwrap(data));
		return NULL;
	}
	static v8::Local<v8::External> toExternal(void* pointer) {
		v8::HandleScope scope;
		return scope.Close(v8::External::New(pointer));
	}

	Arabica::XPath::XPath<std::string>* xpath;
	uscxml::Storage* storage;
};

class V8Exception : public std::runtime_error {
public:

	V8Exception(const std::string& reason) :
		std::runtime_error("DOMException") {
	} // V8Exception

	V8Exception(const V8Exception& rhs) :
		std::runtime_error(rhs),
		reason_(rhs.reason_) {
	} // DOMException

	virtual ~V8Exception() throw() {
	} // DOMBadCast

	virtual const char* what() const throw() {
		return reason_.c_str();
	} // what

private:
	DOMBadCast& operator=(const DOMBadCast&);
	bool operator==(const DOMBadCast&) const;

	std::string reason_;
}; // class DOMException

}
}
#endif /* end of include guard: V8DOM_H_LKE1HKJK */