summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/dom/JSCNodeListCustom.cpp
blob: adb366e5557aec7a637f8a7d99dd9dd82cb02001 (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
#include "JSCNodeList.h"
#include "JSCElement.h"
#include "JSCNode.h"

namespace Arabica {
namespace DOM {

bool JSCNodeList::hasPropertyCustomCallback(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName) {
	size_t propMaxSize = JSStringGetMaximumUTF8CStringSize(propertyName);
	char* propBuffer = new char[propMaxSize];
	JSStringGetUTF8CString(propertyName, propBuffer, propMaxSize);
	std::string propName(propBuffer);
	free(propBuffer);

	std::string base = "0123456789";
	if (propName.find_first_not_of(base) != std::string::npos) {
		return false;
	}

	int index = boost::lexical_cast<int>(propName);
	struct JSCNodeListPrivate* privData = (struct JSCNodeListPrivate*)JSObjectGetPrivate(object);

	if (privData->nativeObj->getLength() < index) {
		return false;
	}

	return true;
}

JSValueRef JSCNodeList::getPropertyCustomCallback(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) {
	size_t propMaxSize = JSStringGetMaximumUTF8CStringSize(propertyName);
	char* propBuffer = new char[propMaxSize];
	JSStringGetUTF8CString(propertyName, propBuffer, propMaxSize);
	std::string propName(propBuffer);
	free(propBuffer);

	std::string base = "0123456789";
	if (propName.find_first_not_of(base) != std::string::npos) {
		return JSValueMakeUndefined(ctx);
	}

	int index = boost::lexical_cast<int>(propName);
	struct JSCNodeListPrivate* privData = (struct JSCNodeListPrivate*)JSObjectGetPrivate(object);
	if (privData->nativeObj->getLength() < index) {
		return JSValueMakeUndefined(ctx);
	}

	switch(privData->nativeObj->item(index).getNodeType()) {
	case Node_base::ELEMENT_NODE: {
		Arabica::DOM::Element<std::string>* retVal = new Arabica::DOM::Element<std::string>(privData->nativeObj->item(index));
		JSClassRef retClass = JSCElement::getTmpl();

		struct JSCElement::JSCElementPrivate* retPrivData = new JSCElement::JSCElementPrivate();
		retPrivData->dom = privData->dom;
		retPrivData->nativeObj = retVal;

		JSObjectRef retObj = JSObjectMake(ctx, retClass, retPrivData);

		return retObj;
		break;
	}
	default: {
		Arabica::DOM::Node<std::string>* retVal = new Arabica::DOM::Node<std::string>(privData->nativeObj->item(index));
		JSClassRef retClass = JSCNode::getTmpl();

		struct JSCNode::JSCNodePrivate* retPrivData = new JSCNode::JSCNodePrivate();
		retPrivData->dom = privData->dom;
		retPrivData->nativeObj = retVal;

		JSObjectRef retObj = JSObjectMake(ctx, retClass, retPrivData);

		return retObj;
	}
	}

	return JSValueMakeUndefined(ctx);
}

#if 0
v8::Handle<v8::Value> V8NodeList::indexedPropertyCustomGetter(uint32_t index, const v8::AccessorInfo &info) {
	v8::Local<v8::Object> self = info.Holder();
	V8NodeListPrivate* privData = V8DOM::toClassPtr<V8NodeListPrivate >(self->GetInternalField(0));

	if (privData->nativeObj->getLength() >= index) {
		switch(privData->nativeObj->item(index).getNodeType()) {
		case Node_base::ELEMENT_NODE: {
			Arabica::DOM::Element<std::string>* retVal = new Arabica::DOM::Element<std::string>(privData->nativeObj->item(index));

			v8::Handle<v8::Function> retCtor = V8Element::getTmpl()->GetFunction();
			v8::Persistent<v8::Object> retObj = v8::Persistent<v8::Object>::New(retCtor->NewInstance());

			struct V8Element::V8ElementPrivate* retPrivData = new V8Element::V8ElementPrivate();
			retPrivData->dom = privData->dom;
			retPrivData->nativeObj = retVal;

			retObj->SetInternalField(0, V8DOM::toExternal(retPrivData));

			retObj.MakeWeak(0, V8Element::jsDestructor);
			return retObj;
		}
		default: {
			Arabica::DOM::Node<std::string>* retVal = new Arabica::DOM::Node<std::string>(privData->nativeObj->item(index));

			v8::Handle<v8::Function> retCtor = V8Node::getTmpl()->GetFunction();
			v8::Persistent<v8::Object> retObj = v8::Persistent<v8::Object>::New(retCtor->NewInstance());

			struct V8Node::V8NodePrivate* retPrivData = new V8Node::V8NodePrivate();
			retPrivData->dom = privData->dom;
			retPrivData->nativeObj = retVal;

			retObj->SetInternalField(0, V8DOM::toExternal(retPrivData));

			retObj.MakeWeak(0, V8Node::jsDestructor);
			return retObj;
		}
		}
	}

	return v8::Undefined();

}
#endif
}
}