Skip to content

Commit df418bd

Browse files
committed
[[ Emscripten ]] Improve bridging of values between JS <-> Engine
Added MCJSObjectRef value type. Implemented conversion between JS & MCValueRef types. Added argument passing to JS eval function, allowing easier scripting.
1 parent 48b0c11 commit df418bd

14 files changed

Lines changed: 658 additions & 118 deletions

engine/engine-sources.gypi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@
560560
#'src/text-segment.cpp',
561561
#'src/text-simplebreakingengine.cpp',
562562

563+
# Group "Emscripten"
564+
'src/jsobject.h',
565+
'src/jsobject.cpp',
566+
563567
# Group "Desktop"
564568
'src/quicktime.cpp',
565569
'src/quicktime.stubs',
@@ -771,6 +775,8 @@
771775
'src/em-filehandle.cpp',
772776
'src/em-fontlist.h',
773777
'src/em-fontlist.cpp',
778+
'src/em-javascript.h',
779+
'src/em-javascript.cpp',
774780
'src/em-liburl.h',
775781
'src/em-liburl.cpp',
776782
'src/em-liburl.js',

engine/src/em-dc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
1919
#include "prefix.h"
2020

2121
#include "em-dc.h"
22+
#include "em-javascript.h"
2223
#include "em-view.h"
2324
#include "em-async.h"
2425
#include "em-event.h"
@@ -118,6 +119,7 @@ Boolean
118119
MCScreenDC::open()
119120
{
120121
return
122+
MCEmscriptenJSInitialize() &&
121123
MCEmscriptenEventInitialize() &&
122124
MCEmscriptenViewInitialize() &&
123125
MCEmscriptenLibUrlInitialize() &&
@@ -132,6 +134,7 @@ MCScreenDC::close(Boolean force)
132134
MCEmscriptenViewFinalize();
133135
MCEmscriptenEventFinalize();
134136
MCEmscriptenLibUrlFinalize();
137+
MCEmscriptenJSFinalize();
135138

136139
return true;
137140
}

engine/src/em-dc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ mergeInto(LibraryManager.library, {
152152
{
153153
this.containerRemoveElement(this._windowList, window.element);
154154
}
155-
LiveCodeUtil.removeObject(pID);
155+
LiveCodeUtil.releaseObject(pID);
156156
}
157157
},
158158

engine/src/em-javascript.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* Copyright (C) 2018 LiveCode Ltd.
2+
3+
This file is part of LiveCode.
4+
5+
LiveCode is free software; you can redistribute it and/or modify it under
6+
the terms of the GNU General Public License v3 as published by the Free
7+
Software Foundation.
8+
9+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
10+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
16+
17+
/*
18+
* Interface to JavaScript:
19+
* Evaluating JS scripts.
20+
* Referencing JS objects, properties & methods.
21+
* Value type conversion.
22+
*/
23+
24+
#include <em-javascript.h>
25+
26+
////////////////////////////////////////////////////////////////////////////////
27+
28+
// Implemented in JavaScript
29+
extern "C" bool MCEmscriptenSystemEvaluateJavaScriptWithArguments(MCStringRef p_script, MCProperListRef p_args, MCValueRef *r_result);
30+
extern "C" void MCEmscriptenUtilReleaseObject(uintptr_t pObjectID);
31+
extern "C" MCJSObjectRef MCEmscriptenSystemWrapHandler(MCHandlerRef pHandler);
32+
33+
////////////////////////////////////////////////////////////////////////////////
34+
35+
extern "C" MC_DLLEXPORT_DEF
36+
bool MCEmscriptenJSEvaluateScriptWithArguments(MCStringRef p_script, MCProperListRef p_args, MCValueRef &r_result)
37+
{
38+
bool t_success = true;
39+
MCAutoValueRef t_result;
40+
t_success = MCEmscriptenSystemEvaluateJavaScriptWithArguments(p_script, p_args, &(&t_result));
41+
42+
if (!t_success)
43+
return MCErrorThrowGeneric((MCStringRef)*t_result);
44+
45+
r_result = t_result.Take();
46+
return true;
47+
}
48+
49+
extern "C" MC_DLLEXPORT_DEF
50+
bool MCEmscriptenJSEvaluateScript(MCStringRef p_script, MCValueRef &r_result)
51+
{
52+
return MCEmscriptenJSEvaluateScriptWithArguments(p_script, kMCEmptyProperList, r_result);
53+
}
54+
55+
////////////////////////////////////////////////////////////////////////////////
56+
57+
extern "C" MC_DLLEXPORT_DEF
58+
bool MCEmscriptenJSWrapHandler(MCHandlerRef p_handler, MCJSObjectRef &r_wrapper)
59+
{
60+
bool t_success = true;
61+
MCJSObjectRef t_wrapper;
62+
t_wrapper = MCEmscriptenSystemWrapHandler(p_handler);
63+
if (t_wrapper == nil)
64+
return MCErrorThrowGeneric(MCSTR("Failed to wrap handler"));
65+
66+
r_wrapper = t_wrapper;
67+
return true;
68+
}
69+
70+
////////////////////////////////////////////////////////////////////////////////
71+
72+
// Callable from JavaScript
73+
extern "C" MC_DLLEXPORT_DEF
74+
MCJSObjectRef MCEmscriptenJSObjectFromID(MCJSObjectID p_id)
75+
{
76+
MCJSObjectRef t_obj;
77+
if (!MCJSObjectCreate(p_id, MCEmscriptenUtilReleaseObject, t_obj))
78+
return nil;
79+
80+
return t_obj;
81+
}
82+
83+
extern "C" MC_DLLEXPORT_DEF
84+
MCJSObjectID MCEmscriptenJSObjectGetID(MCJSObjectRef p_obj)
85+
{
86+
return MCJSObjectGetID(p_obj);
87+
}
88+
89+
extern "C" MC_DLLEXPORT_DEF
90+
bool MCEmscriptenIsJSObject(MCValueRef p_value)
91+
{
92+
return MCValueGetTypeInfo(p_value) == kMCJSObjectTypeInfo;
93+
}
94+
95+
////////////////////////////////////////////////////////////////////////////////
96+
97+
bool MCEmscriptenJSInitialize(void)
98+
{
99+
return true;
100+
}
101+
102+
void MCEmscriptenJSFinalize(void)
103+
{
104+
}
105+
106+
////////////////////////////////////////////////////////////////////////////////

engine/src/em-javascript.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Copyright (C) 2018 LiveCode Ltd.
2+
3+
This file is part of LiveCode.
4+
5+
LiveCode is free software; you can redistribute it and/or modify it under
6+
the terms of the GNU General Public License v3 as published by the Free
7+
Software Foundation.
8+
9+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
10+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
16+
17+
#ifndef __EM_JAVASCRIPT_H__
18+
#define __EM_JAVASCRIPT_H__
19+
20+
////////////////////////////////////////////////////////////////////////////////
21+
22+
#include <jsobject.h>
23+
24+
extern "C" MC_DLLEXPORT
25+
bool MCEmscriptenJSEvaluateScript(MCStringRef p_script, MCValueRef &r_result);
26+
27+
extern "C" MC_DLLEXPORT
28+
bool MCEmscriptenJSEvaluateScriptWithArguments(MCStringRef p_script, MCProperListRef p_args, MCValueRef &r_result);
29+
30+
extern "C" MC_DLLEXPORT
31+
bool MCEmscriptenJSWrapHandler(MCHandlerRef p_handler, MCJSObjectRef &r_wrapper);
32+
33+
////////////////////////////////////////////////////////////////////////////////
34+
35+
bool MCEmscriptenJSInitialize();
36+
void MCEmscriptenJSFinalize();
37+
38+
////////////////////////////////////////////////////////////////////////////////
39+
40+
#endif

engine/src/em-native-layer.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,32 @@
4747
#include "group.h"
4848

4949
#include "em-native-layer.h"
50+
#include "jsobject.h"
5051

5152
////////////////////////////////////////////////////////////////////////////////
5253

53-
extern "C" void MCEmscriptenElementSetRect(int p_element, int p_left, int p_top, int p_right, int p_bottom);
54-
extern "C" void MCEmscriptenElementSetClip(int p_element, int p_left, int p_top, int p_right, int p_bottom);
55-
extern "C" void MCEmscriptenElementSetVisible(int p_element, bool p_visible);
56-
extern "C" void MCEmscriptenElementAddToWindow(int p_element, int p_container);
57-
extern "C" void MCEmscriptenElementRemoveFromWindow(int p_element, int p_container);
58-
extern "C" void MCEmscriptenElementPlaceAbove(int p_element, int p_above, int p_container);
54+
extern "C" void MCEmscriptenElementSetRect(MCJSObjectID p_element, int p_left, int p_top, int p_right, int p_bottom);
55+
extern "C" void MCEmscriptenElementSetClip(MCJSObjectID p_element, int p_left, int p_top, int p_right, int p_bottom);
56+
extern "C" void MCEmscriptenElementSetVisible(MCJSObjectID p_element, bool p_visible);
57+
extern "C" void MCEmscriptenElementAddToWindow(MCJSObjectID p_element, MCJSObjectID p_container);
58+
extern "C" void MCEmscriptenElementRemoveFromWindow(MCJSObjectID p_element, MCJSObjectID p_container);
59+
extern "C" void MCEmscriptenElementPlaceAbove(MCJSObjectID p_element, MCJSObjectID p_above, MCJSObjectID p_container);
5960

6061
////////////////////////////////////////////////////////////////////////////////
6162

62-
inline void MCEmscriptenElementSetRect(int p_element, const MCRectangle &p_rect)
63+
inline void MCEmscriptenElementSetRect(MCJSObjectID p_element, const MCRectangle &p_rect)
6364
{
6465
MCEmscriptenElementSetRect(p_element, p_rect.x, p_rect.y, p_rect.x + p_rect.width, p_rect.y + p_rect.height);
6566
}
6667

67-
inline void MCEmscriptenElementSetClip(int p_element, const MCRectangle &p_rect)
68+
inline void MCEmscriptenElementSetClip(MCJSObjectID p_element, const MCRectangle &p_rect)
6869
{
6970
MCEmscriptenElementSetClip(p_element, p_rect.x, p_rect.y, p_rect.x + p_rect.width, p_rect.y + p_rect.height);
7071
}
7172

7273
////////////////////////////////////////////////////////////////////////////////
7374

74-
MCNativeLayerEmscripten::MCNativeLayerEmscripten(MCObject *p_object, int p_element)
75+
MCNativeLayerEmscripten::MCNativeLayerEmscripten(MCObject *p_object, MCJSObjectID p_element)
7576
{
7677
MCLog("new native layer for %p, %d", p_object, p_element);
7778
m_object = p_object;
@@ -153,7 +154,7 @@ void MCNativeLayerEmscripten::doRelayer()
153154
if (isAttached() && m_object->getstack()->getcard() == m_object->getstack()->getcurcard())
154155
{
155156
// If t_previous_element == 0, this will put the element on the bottom layer
156-
int t_previous_element = 0;
157+
MCJSObjectID t_previous_element = 0;
157158

158159
if (t_previous != nil)
159160
{
@@ -176,16 +177,16 @@ bool MCNativeLayerEmscripten::GetNativeView(void *&r_view)
176177
return true;
177178
}
178179

179-
int MCNativeLayerEmscripten::getStackWindow()
180+
MCJSObjectID MCNativeLayerEmscripten::getStackWindow()
180181
{
181-
return (int)m_object->getstack()->getwindow();
182+
return (MCJSObjectID)m_object->getstack()->getwindow();
182183
}
183184

184185
////////////////////////////////////////////////////////////////////////////////
185186

186187
MCNativeLayer* MCNativeLayer::CreateNativeLayer(MCObject *p_object, void *p_view)
187188
{
188-
return new MCNativeLayerEmscripten(p_object, (int)p_view);
189+
return new MCNativeLayerEmscripten(p_object, MCJSObjectGetID(static_cast<MCJSObjectRef>(p_view)));
189190
}
190191

191192
bool MCNativeLayer::CreateNativeContainer(MCObject *p_object, void *&r_view)

engine/src/em-native-layer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
#define __MC_NATIVE_LAYER_EMSCRIPTEN__
2020

2121
#include "native-layer.h"
22+
#include "jsobject.h"
2223

2324
class MCNativeLayerEmscripten : public MCNativeLayer
2425
{
2526
public:
26-
MCNativeLayerEmscripten(MCObject *p_object, int p_element);
27+
MCNativeLayerEmscripten(MCObject *p_object, MCJSObjectID p_element);
2728
~MCNativeLayerEmscripten();
2829

2930
virtual bool GetCanRenderToContext();
@@ -42,9 +43,9 @@ class MCNativeLayerEmscripten : public MCNativeLayer
4243
// Performs a relayering operation
4344
virtual void doRelayer();
4445

45-
int getStackWindow();
46+
MCJSObjectID getStackWindow();
4647

47-
int m_element;
48+
MCJSObjectID m_element;
4849
};
4950

5051
#endif // ifndef __MC_NATIVE_LAYER_EMSCRIPTEN__

0 commit comments

Comments
 (0)