Exposure document.canvas to javascript api

master
Jeremy Hu 2019-07-27 10:37:07 +09:30
parent 59a59e4fb3
commit a833232d3c
2 changed files with 64 additions and 34 deletions

View File

@ -5,6 +5,7 @@
#include "scriptrunner.h"
#include "util.h"
JSClassID ScriptRunner::js_canvasClassId = 0;
JSClassID ScriptRunner::js_partClassId = 0;
JSClassID ScriptRunner::js_componentClassId = 0;
JSClassID ScriptRunner::js_nodeClassId = 0;
@ -51,7 +52,14 @@ static ScriptRunner::DocumentElement *GetElementFromArg(JSValueConst arg)
ScriptRunner::DocumentComponent *component = (ScriptRunner::DocumentComponent *)JS_GetOpaque(arg,
ScriptRunner::js_componentClassId);
if (nullptr != component) {
element = (ScriptRunner::DocumentComponent *)component;
element = (ScriptRunner::DocumentElement *)component;
}
}
if (nullptr == element) {
ScriptRunner::DocumentCanvas *canvas = (ScriptRunner::DocumentCanvas *)JS_GetOpaque(arg,
ScriptRunner::js_canvasClassId);
if (nullptr != canvas) {
element = (ScriptRunner::DocumentElement *)canvas;
}
}
return element;
@ -520,6 +528,7 @@ void ScriptRunner::run()
countTimeConsumed.start();
// Warning: Not thread safe, but we have only one script instance running, so it doesn't matter
js_canvasClassId = JS_NewClassID(&js_canvasClassId);
js_partClassId = JS_NewClassID(&js_partClassId);
js_componentClassId = JS_NewClassID(&js_componentClassId);
js_nodeClassId = JS_NewClassID(&js_nodeClassId);
@ -542,6 +551,7 @@ void ScriptRunner::run()
JSValue globalObject = JS_GetGlobalObject(context);
JSValue document = JS_NewObject(context);
JS_SetPropertyStr(context,
document, "createComponent",
JS_NewCFunction(context, js_createComponent, "createComponent", 1));
@ -575,6 +585,13 @@ void ScriptRunner::run()
JS_SetPropertyStr(context,
document, "attribute",
JS_NewCFunction(context, js_attribute, "attribute", 2));
JSValue canvas = JS_NewObjectClass(context, ScriptRunner::js_canvasClassId);
JS_SetOpaque(canvas, &m_canvas);
JS_SetPropertyStr(context,
document, "canvas",
canvas);
JS_SetPropertyStr(context, globalObject, "document", document);
JSValue console = JS_NewObject(context);
@ -649,6 +666,8 @@ void ScriptRunner::generateSnapshot()
QStringList rootChildren;
m_resultSnapshot->canvas = m_canvas.attributes;
for (const auto &it: m_components) {
QString idString = it->id;
pointerToIdMap[it] = idString;

View File

@ -16,6 +16,7 @@ public:
enum class DocumentElementType
{
Unknown = 0,
Canvas,
Component,
Part,
Node
@ -56,6 +57,14 @@ public:
DocumentPart *part = nullptr;
};
struct DocumentCanvas : DocumentElement
{
DocumentCanvas()
{
type = DocumentElementType::Canvas;
}
};
ScriptRunner();
~ScriptRunner();
void run();
@ -93,8 +102,10 @@ private:
std::vector<std::pair<DocumentNode *, DocumentNode *>> m_edges;
QString m_scriptError;
QString m_consoleLog;
DocumentCanvas m_canvas;
void generateSnapshot();
public:
static JSClassID js_canvasClassId;
static JSClassID js_partClassId;
static JSClassID js_componentClassId;
static JSClassID js_nodeClassId;