Skip to content

Commit 212726e

Browse files
committed
[V8] Split to V8 and V8API to reuse in Node.js
1 parent 92a9f3b commit 212726e

File tree

12 files changed

+138
-60
lines changed

12 files changed

+138
-60
lines changed

Package.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ let package = Package(
1616
products: [
1717
.library(name: "JavaScript", targets: ["JavaScript"]),
1818
.library(name: "V8", targets: ["V8"]),
19+
.library(name: "V8API", targets: ["V8API"]),
1920
.library(name: "JavaScriptCoreSwift", targets: ["JavaScriptCoreSwift"])
2021
],
2122
dependencies: [
@@ -33,12 +34,18 @@ let package = Package(
3334
.target(
3435
name: "CV8",
3536
dependencies: []),
37+
.target(
38+
name: "CV8Platform",
39+
dependencies: []),
3640
.target(
3741
name: "JavaScriptCoreSwift",
3842
dependencies: ["CJavaScriptCore", "JavaScript"]),
3943
.target(
40-
name: "V8",
44+
name: "V8API",
4145
dependencies: ["CV8", "Platform", "JavaScript"]),
46+
.target(
47+
name: "V8",
48+
dependencies: ["CV8Platform", "V8API", "Platform", "JavaScript"]),
4249
.target(
4350
name: "JavaScript",
4451
dependencies: []),
Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,11 @@
1010

1111
#include <stdlib.h> // malloc, free
1212
#include <string.h> // memset, memcpy
13-
#include <libplatform/libplatform.h>
1413
#include <v8.h>
15-
#include "wrappers.h"
14+
#include "c_v8.h"
1615

1716
using namespace v8;
1817

19-
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
20-
public:
21-
virtual void *Allocate(size_t length){
22-
void *data = AllocateUninitialized(length);
23-
return data == NULL ? data : memset(data, 0, length);
24-
}
25-
virtual void *AllocateUninitialized(size_t length) { return malloc(length); }
26-
virtual void Free(void *data, size_t) { free(data); }
27-
};
28-
2918
class GlobalValue {
3019
public:
3120
explicit GlobalValue(Isolate* isolate, Global<Value>* value):
@@ -61,32 +50,6 @@ class GlobalValue {
6150
};
6251

6352
extern "C" {
64-
ArrayBufferAllocator bufferAllocator;
65-
66-
void* initialize() {
67-
V8::InitializeICU();
68-
auto platform = platform::CreateDefaultPlatform();
69-
V8::InitializePlatform(platform);
70-
V8::Initialize();
71-
return platform;
72-
}
73-
74-
void dispose(void* platform) {
75-
V8::Dispose();
76-
V8::ShutdownPlatform();
77-
delete reinterpret_cast<Platform*>(platform);;
78-
}
79-
80-
void* createIsolate() {
81-
Isolate::CreateParams create_params;
82-
create_params.array_buffer_allocator = &bufferAllocator;
83-
return Isolate::New(create_params);
84-
}
85-
86-
void disposeIsolate(void* isolate) {
87-
reinterpret_cast<Isolate*>(isolate)->Dispose();
88-
}
89-
9053
void* createTemplate(void* isolatePtr) {
9154
auto isolate = reinterpret_cast<Isolate*>(isolatePtr);
9255
Locker isolateLocker(isolate);
@@ -229,7 +192,6 @@ extern "C" {
229192
context->DetachGlobal();
230193

231194
auto newContext = Context::New(isolate, NULL, globalTemplate, globalObject);
232-
233195
globalContext->Reset(isolate, newContext);
234196
}
235197

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* See CONTRIBUTORS.txt for the list of the project authors
99
*/
1010

11-
#ifndef wrappers_h
12-
#define wrappers_h
11+
#ifndef c_v8_h
12+
#define c_v8_h
1313

1414
#include <stdint.h>
1515
#include <stdbool.h>
@@ -18,12 +18,6 @@
1818
extern "C" {
1919
#endif
2020

21-
// global
22-
void * _Nonnull initialize();
23-
void dispose(void * _Nonnull platform);
24-
// isolate
25-
void * _Nonnull createIsolate();
26-
void disposeIsolate(void * _Nonnull isolate);
2721
// global template
2822
void * _Nonnull createTemplate(void * _Nonnull isolate);
2923
void disposeTemplate(void * _Nonnull context);
@@ -68,4 +62,4 @@ extern "C" {
6862
}
6963
#endif
7064

71-
#endif /* wrappers_h */
65+
#endif /* c_v8_h */

Sources/CV8/include/module.modulemap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
module CV8 [system] {
12-
header "wrappers.h"
12+
header "c_v8.h"
1313
link "v8"
1414
export *
1515
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2017 Tris Foundation and the project authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License
6+
*
7+
* See LICENSE.txt in the project root for license information
8+
* See CONTRIBUTORS.txt for the list of the project authors
9+
*/
10+
11+
#include <stdlib.h> // malloc, free
12+
#include <string.h> // memset, memcpy
13+
#include <libplatform/libplatform.h>
14+
#include <v8.h>
15+
#include "c_v8_platrofm.h"
16+
17+
using namespace v8;
18+
19+
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
20+
public:
21+
virtual void *Allocate(size_t length){
22+
void *data = AllocateUninitialized(length);
23+
return data == NULL ? data : memset(data, 0, length);
24+
}
25+
virtual void *AllocateUninitialized(size_t length) { return malloc(length); }
26+
virtual void Free(void *data, size_t) { free(data); }
27+
};
28+
29+
extern "C" {
30+
ArrayBufferAllocator bufferAllocator;
31+
32+
void* initialize() {
33+
V8::InitializeICU();
34+
auto platform = platform::CreateDefaultPlatform();
35+
V8::InitializePlatform(platform);
36+
V8::Initialize();
37+
return platform;
38+
}
39+
40+
void dispose(void* platform) {
41+
V8::Dispose();
42+
V8::ShutdownPlatform();
43+
delete reinterpret_cast<Platform*>(platform);;
44+
}
45+
46+
void* createIsolate() {
47+
Isolate::CreateParams create_params;
48+
create_params.array_buffer_allocator = &bufferAllocator;
49+
return Isolate::New(create_params);
50+
}
51+
52+
void disposeIsolate(void* isolate) {
53+
reinterpret_cast<Isolate*>(isolate)->Dispose();
54+
}
55+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2017 Tris Foundation and the project authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License
6+
*
7+
* See LICENSE.txt in the project root for license information
8+
* See CONTRIBUTORS.txt for the list of the project authors
9+
*/
10+
11+
#ifndef c_v8_platrofm_h
12+
#define c_v8_platrofm_h
13+
14+
#include <stdint.h>
15+
#include <stdbool.h>
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
// global
22+
void * _Nonnull initialize();
23+
void dispose(void * _Nonnull platform);
24+
// isolater
25+
void * _Nonnull createIsolate();
26+
void disposeIsolate(void * _Nonnull isolate);
27+
28+
#ifdef __cplusplus
29+
}
30+
#endif
31+
32+
#endif /* c_v8_platrofm_h */
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright 2017 Tris Foundation and the project authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License
6+
*
7+
* See LICENSE.txt in the project root for license information
8+
* See CONTRIBUTORS.txt for the list of the project authors
9+
*/
10+
11+
module CV8Platform [system] {
12+
header "c_v8_platrofm.h"
13+
link "v8"
14+
export *
15+
}

Sources/V8/JSEngine.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* See CONTRIBUTORS.txt for the list of the project authors
99
*/
1010

11-
import CV8
1211
import JavaScript
1312

1413
public class JSEngine: JavaScript.JSEngine {

Sources/V8/JSRuntime.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
* See CONTRIBUTORS.txt for the list of the project authors
99
*/
1010

11-
import CV8
11+
import CV8Platform
1212
import JavaScript
1313

14+
@_exported import V8API
15+
1416
public class JSRuntime {
1517
let platform: UnsafeMutableRawPointer
1618
let isolate: UnsafeMutableRawPointer
@@ -20,9 +22,11 @@ public class JSRuntime {
2022
}()
2123

2224
public required init() {
25+
// The whole V8API & V8 split is caused
26+
// by difference in this initialization
27+
// between standalone v8 and node.js
2328
self.platform = initialize()
2429
self.isolate = createIsolate()
25-
CV8.swiftCallback = functionWrapper
2630
}
2731

2832
deinit {
@@ -31,8 +35,14 @@ public class JSRuntime {
3135
}
3236
}
3337

38+
extension V8API.JSContext {
39+
public convenience init(_ runtime: JSRuntime = JSRuntime.global) {
40+
self.init(isolate: runtime.isolate)
41+
}
42+
}
43+
3444
extension JSRuntime: JavaScript.JSRuntime {
35-
public func createContext() -> JSContext {
36-
return JSContext(self)
45+
public func createContext() -> V8API.JSContext {
46+
return JSContext()
3747
}
3848
}

0 commit comments

Comments
 (0)