1 module awebview.wrapper.jsobject;
2 
3 import std.range;
4 import std.utf;
5 
6 import awebview.wrapper.cpp;
7 import awebview.wrapper.jsarray : JSArray, JSArrayCpp;
8 import awebview.wrapper.jsvalue : JSValue;
9 import awebview.wrapper.webstring : WebString, WebStringCpp;
10 import awebview.wrapper.constants;
11 import awebview.wrapper.weakref;
12 import awebview.wrapper.cpp.mmanager;
13 
14 public import awebview.wrapper.cpp : JSObjectType;
15 
16 import carbon.nonametype;
17 
18 debug import std.stdio;
19 
20 struct JSObject
21 {
22     ~this() nothrow @nogc
23     {
24         if(this._field.instance_.local !is null)
25             JSObjectMember.dtor(this.cppObj!false);
26     }
27 
28 
29     this(this) nothrow @nogc
30     {
31         JSObject copy;
32         JSObjectMember.ctor(copy.cppObj!false, this.cppObj);
33         this._field = copy._field;
34         copy._field.instance_.local = null;
35     }
36 
37 
38     @property
39     CppObj cppObj(bool withInitialize = true)() nothrow @trusted @nogc
40     {
41         CppObj ret = cast(CppObj)cast(void*)&_field;
42 
43       static if(withInitialize)
44         if(_field.instance_.local is null)
45             JSObjectMember.ctor(ret);
46 
47         return ret;
48     }
49 
50 
51     @property
52     inout(CppObj) cppObj(this T)() inout nothrow @trusted @nogc
53     if(is(T == const) || is(T == immutable))
54     {
55         if(_field.instance_.local is null)
56             return cast(inout(CppObj))cast(inout(void)*)&JSObjectConsts._emptyInstance._field;
57         else
58             return cast(inout(CppObj))cast(inout(void)*)&_field;
59     }
60 
61 
62     @property
63     uint remoteId() const nothrow @nogc
64     {
65         return JSObjectMember.remote_id(this.cppObj);
66     }
67 
68 
69     @property
70     int refCount() const nothrow @nogc
71     {
72         return JSObjectMember.ref_count(this.cppObj);
73     }
74 
75 
76     @property
77     JSObjectType type() const nothrow @nogc
78     {
79         return JSObjectMember.type(this.cppObj);
80     }
81 
82 
83     @property
84     JSArray propertyNames() const nothrow @nogc
85     {
86         JSArray ja;
87         JSObjectMember.GetPropertyNames(this.cppObj, ja.cppObj);
88         return ja;
89     }
90 
91 
92     bool hasProperty(string str) const nothrow @nogc
93     {
94         //WebString ws = str;
95         return hasProperty(WebString(str));
96     }
97 
98 
99     bool hasProperty(in WebString str) const nothrow @nogc
100     {
101         return JSObjectMember.HasProperty(this.cppObj, str.cppObj);
102     }
103 
104 
105     JSValue getProperty(string str) const nothrow @nogc
106     {
107         //WebString ws = str;
108         return getProperty(WebString(str));
109     }
110 
111 
112     JSValue getProperty(in WebString str) const nothrow @nogc
113     {
114         JSValue jv;
115         JSObjectMember.GetProperty(this.cppObj, str.cppObj, jv.cppObj);
116         return jv;
117     }
118 
119 
120     void setProperty(in string str, in JSValue value) nothrow @nogc
121     {
122         setProperty(WebString(str), value);
123     }
124 
125 
126     void setProperty(in WebString str, in JSValue value) nothrow @nogc
127     {
128         JSObjectMember.SetProperty(this.cppObj, str.cppObj, value.cppObj);
129     }
130 
131 
132     void opIndexAssign(T)(T v, string str)
133     if(is(typeof(JSValue(v)) == JSValue))
134     {
135         setProperty(str, v);
136     }
137 
138 
139     JSValue opIndex(string str)
140     {
141         return getProperty(str);
142     }
143 
144 
145     void setPropertyAsync(in string str, in JSObject value) nothrow @nogc
146     {
147         //WebString ws = str;
148         setPropertyAsync(WebString(str), value);
149     }
150 
151 
152     void setPropertyAsync(in WebString str, in JSObject value) nothrow @nogc
153     {
154         JSObjectMember.SetPropertyAsync(this.cppObj, str.cppObj, value.cppObj);
155     }
156 
157 
158     void removeProperty(in string str) nothrow @nogc
159     {
160         //WebString ws = str;
161         removeProperty(WebString(str));
162     }
163 
164 
165     void removeProperty(in WebString str) nothrow @nogc
166     {
167         JSObjectMember.RemoveProperty(this.cppObj, str.cppObj);
168     }
169 
170 
171     @property
172     JSArray getMethodNames() const nothrow @nogc
173     {
174         JSArray ja;
175         JSObjectMember.GetMethodNames(this.cppObj, ja.cppObj);
176         return ja;
177     }
178 
179 
180     @property
181     bool hasMethod(Char)(in Char[] str) const nothrow @nogc
182     {
183         //WebString ws = str;
184         return hasMethod(WebString(str));
185     }
186 
187 
188     @property
189     bool hasMethod(in WebString str) const nothrow @nogc
190     {
191         return JSObjectMember.HasMethod(this.cppObj, str.cppObj);
192     }
193 
194 
195     JSValue invoke(in WebString str, in JSArray args) nothrow @nogc
196     {
197         JSValue jv;
198         JSObjectMember.Invoke(this.cppObj, str.cppObj, args.cppObj, jv.cppObj);
199         return jv;
200     }
201 
202 
203     void invokeAsync(in WebString str, in JSArray args) nothrow @nogc
204     {
205         JSObjectMember.InvokeAsync(this.cppObj, str.cppObj, args.cppObj);
206     }
207 
208 
209     void toString(scope void delegate(const(char)[]) sink) const
210     {
211         WebString str;
212         JSObjectMember.ToString(this.cppObj, str.cppObj);
213         foreach(char e; str.data.byChar)
214             put(sink, e);
215     }
216 
217 
218     void setCustomMethod(in WebString str, bool hasReturnValue) @nogc nothrow
219     {
220         JSObjectMember.SetCustomMethod(this.cppObj, str.cppObj, hasReturnValue);
221     }
222 
223 
224     void setCustomMethod(in string str, bool hasReturnValue) @nogc nothrow
225     {
226         //WebString ws = str;
227         setCustomMethod(WebString(str), hasReturnValue);
228     }
229 
230 
231     @property
232     awebview.wrapper.cpp.Awesomium.Error lastError() const nothrow @nogc
233     {
234         return JSObjectMember.last_error(this.cppObj);
235     }
236 
237 
238     static
239     auto weakRef(H)(H ws)
240     if(is(H : const(CppObj)))
241     {
242       static if(is(H == CppObj))
243         JSObject* wsp = cast(JSObject*)cast(void*)ws;
244       else static if(is(H == const(CppObj)))
245         const(JSObject)* wsp = cast(const(JSObject)*)cast(const(void)*)ws;
246       else
247         immutable(JSObject)* wsp = cast(immutable(JSObject)*)cast(immutable(void)*)ws;
248 
249         return refP(wsp);
250     }
251 
252   private:
253     alias CppObj = awebview.wrapper.cpp.JSObject;
254     CppObj.Field _field;
255 }
256 
257 
258 abstract class JSMethodHandler : awebview.wrapper.cpp.IJSMethodHandlerD
259 {
260     this()
261     {
262         auto id = MemoryManager.instance.register(cast(void*)this);
263         _cppObj = JSMethodHandlerD2CppMember.newCtor(this, id);
264     }
265 
266 
267     ~this()
268     {
269         JSMethodHandlerD2CppMember.deleteDtor(_cppObj);
270     }
271 
272 
273     @property
274     inout(CppObj) cppObj() inout pure nothrow @safe @nogc
275     {
276         return _cppObj;
277     }
278 
279 
280   extern(C++)
281   {
282     void call(awebview.wrapper.cpp.WebView view,
283               uint objId, const(awebview.wrapper.cpp.WebString) methodName,
284               const(awebview.wrapper.cpp.JSArray) args)
285     {
286         onCall(view, objId, methodName.weakRef!WebStringCpp, args.weakRef!JSArrayCpp);
287     }
288 
289 
290     void callWithReturnValue(
291         awebview.wrapper.cpp.WebView view,
292         uint objId, const(awebview.wrapper.cpp.WebString) methodName,
293         const(awebview.wrapper.cpp.JSArray) args,
294         awebview.wrapper.cpp.JSValue dst)
295     {
296         dst.weakRef!JSValue = onCallWithRV(view, objId, methodName.weakRef!WebStringCpp, args.weakRef!JSArrayCpp);
297     }
298   }
299 
300 
301     void onCall(awebview.wrapper.cpp.WebView view,
302                 uint objId, WeakRef!(const(WebStringCpp)) methodName,
303                 WeakRef!(const(JSArrayCpp)) args);
304 
305 
306     JSValue onCallWithRV(
307         awebview.wrapper.cpp.WebView view,
308         uint objId, WeakRef!(const(WebStringCpp)) methodName,
309         WeakRef!(const(JSArrayCpp)) args);
310 
311 
312   private:
313     CppObj _cppObj;
314 
315     alias CppObj = awebview.wrapper.cpp.JSMethodHandlerD2Cpp;
316 }