Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions qtwrapper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Qt Wrapper Sample (for Shared Library)
This sample shows how to wrap Qt's objects in order to embed them in ChakraCore.
It shows how to retrieve and set values from C++ world.

1. Initializing the JavaScript runtime and creating an execution context.
2. Running scripts and handling values.
3. Printing out script results.

## Requirements
This code was tested with Qt 5.10 and ChakraCore 1.9 (not the stable version, the master, commit 3f7691187ba02ab2a1079619fed867714786b693, at Feb 10 14:50:00 2018 -0800), under Linux.

This code should run with "any" Qt version as there is no fancy things.


## Build and run the sample
1. [Clone](https://github.com/Microsoft/ChakraCore) and [build](https://github.com/Microsoft/ChakraCore/wiki/Building-ChakraCore) ChakraCore on designated platform.
From here we will assume that ChakraCore was cloned in /home/xxx/ChakraCore, and built a shared library (the default build setting). If you cloned elsewhere or are under Windows, you have to adapt accordingly the qtwrapper.pro file.
2. then get in the qtwrapper directory, and `qmake && make` and it is done:
`LD_LIBRARY_PATH='/home/xxx/ChakraCore/out/Release' ./wrappersample`

## side note
this Qt's wrapper allows:
- to access from ChakraCore to Qt's Object
- set variables from C++
- get variables from ChakraCore

At least even though you're not interested by Qt, you will have examples for:
- access to variables from ChakraCore: see JSEngine::registerValue(const QString&, const QVariant&), toJsValueRef; both are in qjsengine.cpp
- register attribute's object and be able to get notified when they are changed; see:
- JSEngine::JSEngine, that register the handler that will be called by the futur object's proxy
- JSEngine::registerValue(QObject*, const QString&) that:
- register one getter and one setter (see the set and get functions in the same file)
- set the proxy by associating the handler and the object
27 changes: 27 additions & 0 deletions qtwrapper/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <QDebug>
#include "qtwrapper/qjsengine.h"
#include "qtobject.h"

int main()
{
QVariant result;
QStringList list;
QMap<QString, QVariant> map;
QtObject *snouf = new QtObject;

list << "a" << "b" << "c" << "d" << "1" << "2";
map["e"] = "f";map["g"] = "h";map["i"] = 3;map["4"] = "5";


chakracorejsengine::JSEngine jsEngine;
jsEngine.registerValue("nb", 46); //register a simple value
jsEngine.registerValue(snouf, "coucou"); //register a Qt's Object
jsEngine.registerValue("list", list); //register a list (as an array)
jsEngine.registerValue("map", map); //register a map (as an objet)
jsEngine.evaluate("coucou.echo('access to all variables: nb='+nb+', list[2]='+list[2]+', map.g='+map.g);");
if (jsEngine.errorEncountered()) return EXIT_FAILURE;
result = jsEngine.evaluate("5"); //access to value returned
qDebug("Value returned: %d", result.toInt());

return EXIT_SUCCESS;
}
10 changes: 10 additions & 0 deletions qtwrapper/qtobject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "qtobject.h"

QtObject::QtObject(QObject *parent) : QObject(parent)
{

}
void QtObject::echo(const QString& message)
{
qDebug("Internal value name=%s, message=%s", name.toUtf8().data(), message.toUtf8().data());
}
21 changes: 21 additions & 0 deletions qtwrapper/qtobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef QTOBJECT_H
#define QTOBJECT_H

#include <QObject>

class QtObject : public QObject
{
Q_OBJECT
QString name;
public:
Q_PROPERTY(QString name READ getName WRITE setName)
explicit QtObject(QObject *parent = nullptr);
QString getName()const{return name;}
void setName(const QString& name){this->name = name;}
signals:

public slots:
void echo(const QString& message);
};

#endif // QTOBJECT_H
34 changes: 34 additions & 0 deletions qtwrapper/qtwrapper.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
QT += core
QT -= gui

CONFIG += c++17 -rdynamic console
unix {
QMAKE_CXXFLAGS += -std=c++17 -rdynamic
}
TARGET = wrappersample
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp \
qtwrapper/qjsengine.cpp \
qtobject.cpp

DEFINES += QT_DEPRECATED_WARNINGS


INCLUDEPATH += /home/melidrissi/.root/Applications/ChakraCore/lib/Jsrt/
HEADERS += \
qtwrapper/qjsengine.h \
qtobject.h

unix {
LIBS += -L/home/melidrissi/.root/Applications/ChakraCore/out/Release -lChakraCore -pthread -lm -ldl -licuuc
}
win32 {
LIBS += 'C:/PATH_TO_LIBRARY/ChakraCore.Lib'
INCLUDEPATH += PATH_TO_LIBRARY_INCLUDES/include
}

DISTFILES += \
README.md
Loading