Skip to content

Commit

Permalink
core, feat: process background tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Mar 5, 2023
1 parent 2805641 commit ef270ca
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
9 changes: 7 additions & 2 deletions fibjs/src/base/Isolate.cpp
Expand Up @@ -28,19 +28,24 @@ void init_process_ipc(Isolate* isolate);
exlib::LockedList<Isolate> s_isolates;
exlib::atomic s_iso_id;
exlib::atomic s_iso_ref;
extern v8::Platform* g_default_platform;

static int32_t syncRunMicrotasks(Isolate* isolate)
{
JSFiber::EnterJsScope s;

isolate->m_isolate->PerformMicrotaskCheckpoint();
while (v8::platform::PumpMessageLoop(g_default_platform, isolate->m_isolate,
isolate->m_isolate->HasPendingBackgroundTasks()
? v8::platform::MessageLoopBehavior::kWaitForWork
: platform::MessageLoopBehavior::kDoNotWait))
isolate->m_isolate->PerformMicrotaskCheckpoint();

return 0;
}

void Isolate::RunMicrotasks()
{
if (RunMicrotaskSize(m_isolate) > 0)
if (RunMicrotaskSize(m_isolate) > 0 || m_isolate->HasPendingBackgroundTasks())
syncCall(this, syncRunMicrotasks, this);
}

Expand Down
4 changes: 3 additions & 1 deletion fibjs/src/base/Runtime.cpp
Expand Up @@ -29,6 +29,7 @@ void options(int32_t& pos, char* argv[]);
result_t ifZipFile(exlib::string filename, bool& retVal);

exlib::string s_root;
v8::Platform* g_default_platform;

static void createBasisForFiberLoop(Isolate::platform_creator get_platform)
{
Expand All @@ -53,7 +54,8 @@ static void createBasisForFiberLoop(Isolate::platform_creator get_platform)

srand((unsigned int)time(0));

v8::V8::InitializePlatform(get_platform ? get_platform() : v8::platform::NewDefaultPlatform().release());
g_default_platform = get_platform ? get_platform() : v8::platform::NewDefaultPlatform().release();
v8::V8::InitializePlatform(g_default_platform);
v8::V8::Initialize();
}

Expand Down
Binary file removed test/wasm/math.wasm
Binary file not shown.
38 changes: 25 additions & 13 deletions test/wasm_test.js
@@ -1,13 +1,22 @@
const test = require("test");
test.setup();

const fs = require('fs');
const path = require('path');
const util = require('util');

const wasmDir = path.join(__dirname, 'wasm');

function loadWebAssembly(name) {
var code = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 0, 12, 6, 100, 121, 108,
105, 110, 107, 128, 128, 192, 2, 0, 1, 137, 128, 128, 128, 0, 2, 96, 1, 127,
1, 127, 96, 0, 0, 2, 193, 128, 128, 128, 0, 4, 3, 101, 110, 118, 10, 109, 101,
109, 111, 114, 121, 66, 97, 115, 101, 3, 127, 0, 3, 101, 110, 118, 6, 109,
101, 109, 111, 114, 121, 2, 0, 128, 2, 3, 101, 110, 118, 5, 116, 97, 98,
108, 101, 1, 112, 0, 0, 3, 101, 110, 118, 9, 116, 97, 98, 108, 101, 66, 97,
115, 101, 3, 127, 0, 3, 132, 128, 128, 128, 0, 3, 0, 1, 1, 6, 139, 128, 128,
128, 0, 2, 127, 1, 65, 0, 11, 127, 1, 65, 0, 11, 7, 174, 128, 128, 128, 0,
3, 18, 95, 95, 112, 111, 115, 116, 95, 105, 110, 115, 116, 97, 110, 116,
105, 97, 116, 101, 0, 2, 7, 95, 115, 113, 117, 97, 114, 101, 0, 0, 11, 114,
117, 110, 80, 111, 115, 116, 83, 101, 116, 115, 0, 1, 9, 129, 128, 128, 128,
0, 0, 10, 175, 128, 128, 128, 0, 3, 135, 128, 128, 128, 0, 0, 32, 0, 32, 0,
108, 11, 131, 128, 128, 128, 0, 0, 1, 11, 149, 128, 128, 128, 0, 0, 2, 64, 35,
0, 36, 2, 35, 2, 65, 128, 128, 192, 2, 106, 36, 3, 16, 1, 11, 11]);

function imports() {
var imports = {};
imports.env = imports.env || {}

Expand All @@ -26,18 +35,21 @@ function loadWebAssembly(name) {
})
}

return new WebAssembly.Instance(
new WebAssembly.Module(fs.readFile(path.join(wasmDir, name))),
imports
);
return imports;
}

describe("wasm", () => {
it('load module', () => {
var instance = loadWebAssembly('math.wasm');
var instance = new WebAssembly.Instance(new WebAssembly.Module(code), imports());
var math = instance.exports;
assert.equal(math._square(3), 9);
});

it('async load module', async () => {
var instance = new WebAssembly.Instance(await WebAssembly.compile(code), imports());
var math = instance.exports;
assert.equal(math._square(3), 9);
});
});

require.main === module && test.run(console.DEBUG);
require.main === module && test.run(console.DEBUG);

0 comments on commit ef270ca

Please sign in to comment.