Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjipeng committed Oct 29, 2024
1 parent 0be42c8 commit d316b00
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 38 deletions.
51 changes: 51 additions & 0 deletions src/core/global.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2024, Zhang Ji Peng <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include "global.h"

namespace mescal {

static GlobalData _globalData;

const GlobalData* const _global(void)
{
return &_globalData;
}

bool _global_initialize(void)
{
//FIXME: add setup parameters
_globalData.runtime = JS_NewRuntime();
return true;
}

void _global_shutdown(void)
{
JS_FreeRuntime(_globalData.runtime);
}

}
52 changes: 52 additions & 0 deletions src/core/global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2024, Zhang Ji Peng <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _SYSTEM_GLOBAL_H_
#define _SYSTEM_GLOBAL_H_

#include <stdint.h>
#include <stddef.h>

#include <quickjs.h>

namespace mescal {

#define GLOBAL() mescal::_global()


typedef struct {
// quick javascript runtime
JSRuntime* runtime;
} GlobalData;

const GlobalData* const _global(void);

bool _global_initialize(void);

void _global_shutdown(void);

}
#endif /*_SYSTEM_GLOBAL_H_*/
2 changes: 2 additions & 0 deletions src/src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ file(GLOB_RECURSE QJS_SOURCES ${PROJ_ROOT}/src/webcore/buildQJS/*.cpp
${PROJ_ROOT}/src/webcore/buildQJS/*.c
${PROJ_ROOT}/src/webcore/bindings/qjs/*.cpp
${PROJ_ROOT}/src/wtf/*.cpp
${PROJ_ROOT}/src/core/*.cpp
)

set(SOURCES ${SOURCES} ${QJS_SOURCES})
Expand Down Expand Up @@ -118,6 +119,7 @@ elseif (OPT_USE_QJS)
set(QJS_HEADERS ${PROJ_ROOT}/src/wtf
${PROJ_ROOT}/src/webcore/buildQJS
${PROJ_ROOT}/src/webcore/bindings/qjs
${PROJ_ROOT}/src/core
${PROJ_ROOT}/src/javascript # tmp include
)

Expand Down
25 changes: 5 additions & 20 deletions src/webcore/bindings/qjs/qjs_binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
#include <wtf/PassRefPtr.h>
#include <wtf/RefPtr.h>

#include <Shared.h>
#include <text/String.h>

#include <quickjs.h>

#include "global.h"

namespace WebCore {
class AtomicString;
class Document;
Expand All @@ -48,28 +51,14 @@ namespace WebCore {
}

namespace QJS {

/**
* Base class for all objects in this binding.
*/
class DOMObject {
protected:
DOMObject()
{
}
#ifndef NDEBUG
virtual ~DOMObject();
#endif
};

/**
* We inherit from Interpreter, to save a pointer to the HTML part
* that the interpreter runs for.
* The interpreter also stores the DOM object -> KJS::DOMObject cache.
*/
class ScriptInterpreter {
class ScriptInterpreter : public WebCore::Shared<ScriptInterpreter> {
public:
ScriptInterpreter(JSValue global, WebCore::Frame*);
virtual ~ScriptInterpreter() { }

static JSValue getDOMObject(ScriptInterpreter*, void* objectHandle);
static void putDOMObject(ScriptInterpreter*, void* objectHandle, JSValue);
Expand All @@ -91,7 +80,6 @@ namespace QJS {
* Set the event that is triggering the execution of a script, if any
*/
void setCurrentEvent(WebCore::Event* event) { m_currentEvent = event; }
void setInlineCode(bool inlineCode) { m_inlineCode = inlineCode; }
void setProcessingTimerCallback(bool timerCallback) { m_timerCallback = timerCallback; }


Expand All @@ -109,11 +97,8 @@ namespace QJS {
virtual bool shouldInterruptScript() const;

private:
virtual ~ScriptInterpreter() { } // only deref on the base class should delete us

WebCore::Frame* m_frame;
WebCore::Event* m_currentEvent;
bool m_inlineCode;
bool m_timerCallback;
};

Expand Down
25 changes: 8 additions & 17 deletions src/webcore/bindings/qjs/qjs_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ ScriptController::~ScriptController()

if (m_script) {
m_script = 0;

JS_FreeContext(m_context);

// It's likely that destroying the interpreter has created a lot of garbage.
gcController().garbageCollectSoon();
Expand All @@ -76,13 +78,6 @@ JSValue ScriptController::evaluate(const String& filename, int baseLine, const S
// if there was none, an error occured or the type couldn't be converted.

initScriptIfNeeded();
// inlineCode is true for <a href="javascript:doSomething()">
// and false for <script>doSomething()</script>. Check if it has the
// expected value in all cases.
// See smart window.open policy for where this is used.
bool inlineCode = filename.isNull();

m_script->setInlineCode(inlineCode);

// Evaluating the JavaScript could cause the frame to be deallocated
// so we start the keep alive timer here.
Expand Down Expand Up @@ -175,23 +170,19 @@ void ScriptController::initScriptIfNeeded()
if (m_script)
return;

m_context = JS_NewContext(GLOBAL()->runtime);

// Build the global object - which is a Window instance
JSObject* globalObject = new JSDOMWindow(m_frame->domWindow());
JSDOMWindow::init(m_context);
JSValue contextObj = JS_GetGlobalObject(m_context);
JSValue globalObject = JSDOMWindow::create(m_context, contextObj, m_frame->domWindow());
JS_FreeValue(m_context, contextObj);

// Create a KJS interpreter for this frame
m_script = new ScriptInterpreter(globalObject, m_frame);

GCController::init(JS_GetRuntime(m_script->context()));

//init dom object all Quickjs !!! <Debug>

String userAgent = m_frame->loader()->userAgent(m_frame->document() ? m_frame->document()->URL() : KURL());
if (userAgent.find("Microsoft") >= 0 || userAgent.find("MSIE") >= 0)
m_script->setCompatMode(Interpreter::IECompat);
else
// If we find "Mozilla" but not "(compatible, ...)" we are a real Netscape
if (userAgent.find("Mozilla") >= 0 && userAgent.find("compatible") == -1)
m_script->setCompatMode(Interpreter::NetscapeCompat);

m_frame->loader()->dispatchWindowObjectAvailable();

Expand Down
2 changes: 1 addition & 1 deletion src/webcore/bindings/qjs/qjs_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace QJS {

struct WindowPrivate;

class Window : public DOMObject {
class Window {
friend class Location;
friend class WindowFunc;
friend class ScheduledAction;
Expand Down
12 changes: 12 additions & 0 deletions src/webview/WebEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#include "GCController.h"
#include "Cache.h"

#if ENABLE(QJS)
#include "global.h"
#endif

namespace WebCore {
extern void setPixelFormat(_mc_format format);
extern void setScreenSize(int w, int h);
Expand Down Expand Up @@ -878,6 +882,11 @@ MC_STATUS macross_initialize(MC_PIXEL_FORMAT format, int w, int h)
if (!ps_initialize())
return MC_STATUS_FAILED;

#if ENABLE(QJS)
mescal::_global_initialize();
GCController::init(GLOBAL()->runtime);
#endif

WebCore::setPixelFormat(format);
WebCore::setScreenSize(w, h);
WebCore::eventInitialize();
Expand All @@ -893,6 +902,9 @@ void macross_shutdown(void)
if (g_initialize) {
WebCore::eventShatdown();
globalDataSave();
#if ENABLE(QJS)
mescal::_global_shutdown();
#endif
ps_shutdown();
g_initialize = false;
}
Expand Down

0 comments on commit d316b00

Please sign in to comment.