Skip to content

Commit f39a3f1

Browse files
liamappelbecommit-bot@chromium.org
authored andcommitted
Scaffolding for dart:wasm
This CL doesn't have any tests because it's just boilerplate. I'll add a test in the follow up CLs where I add actual functionality. Bug: #37882 Change-Id: I47c81f5f1be724f8226e756ba5d01880a45f1ac7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112841 Reviewed-by: Siva Annamalai <[email protected]> Reviewed-by: Liam Appelbe <[email protected]> Commit-Queue: Liam Appelbe <[email protected]>
1 parent 3729b96 commit f39a3f1

17 files changed

+110
-4
lines changed

pkg/vm/lib/target/vm.dart

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class VmTarget extends Target {
7676
'dart:nativewrappers',
7777
'dart:io',
7878
'dart:cli',
79+
'dart:wasm',
7980
];
8081

8182
@override

runtime/lib/wasm.cc

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
#include "platform/unicode.h"
6+
#include "vm/bootstrap_natives.h"
7+
#include "vm/dart_entry.h"
8+
9+
namespace dart {
10+
11+
int callWasm(const char* name, int n) {
12+
return 100 * n;
13+
}
14+
15+
// This is a temporary API for prototyping.
16+
DEFINE_NATIVE_ENTRY(Wasm_callFunction, 0, 2) {
17+
GET_NON_NULL_NATIVE_ARGUMENT(String, fn_name, arguments->NativeArgAt(0));
18+
GET_NON_NULL_NATIVE_ARGUMENT(Integer, arg, arguments->NativeArgAt(1));
19+
20+
intptr_t len = Utf8::Length(fn_name);
21+
std::unique_ptr<char> name = std::unique_ptr<char>(new char[len + 1]);
22+
fn_name.ToUTF8(reinterpret_cast<uint8_t*>(name.get()), len);
23+
name.get()[len] = 0;
24+
25+
return Smi::New(callWasm(name.get(), arg.AsInt64Value()));
26+
}
27+
28+
} // namespace dart

runtime/lib/wasm_patch.dart

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import "dart:_internal" show patch;
6+
7+
@patch
8+
int _callWasm(String name, int arg) native "Wasm_callFunction";

runtime/lib/wasm_sources.gni

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
wasm_runtime_cc_files = [ "wasm.cc" ]
6+
7+
wasm_runtime_dart_files = [ "wasm_patch.dart" ]

runtime/vm/BUILD.gn

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import("../../sdk/lib/mirrors/mirrors_sources.gni")
1616
import("../../sdk/lib/profiler/profiler_sources.gni")
1717
import("../../sdk/lib/typed_data/typed_data_sources.gni")
1818
import("../../sdk/lib/vmservice/vmservice_sources.gni")
19+
import("../../sdk/lib/wasm/wasm_sources.gni")
1920
import("../../utils/compile_platform.gni")
2021
import("../bin/cli_sources.gni")
2122
import("../bin/io_sources.gni")
@@ -33,6 +34,7 @@ import("../lib/mirrors_sources.gni")
3334
import("../lib/profiler_sources.gni")
3435
import("../lib/typed_data_sources.gni")
3536
import("../lib/vmservice_sources.gni")
37+
import("../lib/wasm_sources.gni")
3638
import("../runtime_args.gni")
3739
import("compiler/compiler_sources.gni")
3840
import("heap/heap_sources.gni")
@@ -106,7 +108,7 @@ library_for_all_configs("libdart_lib") {
106108
internal_runtime_cc_files + isolate_runtime_cc_files +
107109
math_runtime_cc_files + mirrors_runtime_cc_files +
108110
typed_data_runtime_cc_files + vmservice_runtime_cc_files +
109-
ffi_runtime_cc_files
111+
ffi_runtime_cc_files + wasm_runtime_cc_files
110112
sources = [ "bootstrap.cc" ] + rebase_path(allsources, ".", "../lib")
111113
snapshot_sources = []
112114
nosnapshot_sources = []

runtime/vm/bootstrap_natives.cc

+5
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ void Bootstrap::SetupNativeResolver() {
141141
ASSERT(!library.IsNull());
142142
library.set_native_entry_resolver(resolver);
143143
library.set_native_entry_symbol_resolver(symbol_resolver);
144+
145+
library = Library::WasmLibrary();
146+
ASSERT(!library.IsNull());
147+
library.set_native_entry_resolver(resolver);
148+
library.set_native_entry_symbol_resolver(symbol_resolver);
144149
}
145150

146151
bool Bootstrap::IsBootstrapResolver(Dart_NativeEntryResolver resolver) {

runtime/vm/bootstrap_natives.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace dart {
1313

1414
// List of bootstrap native entry points used in the core dart library.
15+
// V(function_name, argument_count)
1516
#define BOOTSTRAP_NATIVE_LIST(V) \
1617
V(AsyncStarMoveNext_debuggerStepCheck, 1) \
1718
V(DartAsync_fatal, 1) \
@@ -392,7 +393,8 @@ namespace dart {
392393
V(Ffi_dl_processLibrary, 0) \
393394
V(Ffi_dl_executableLibrary, 0) \
394395
V(TransferableTypedData_factory, 2) \
395-
V(TransferableTypedData_materialize, 1)
396+
V(TransferableTypedData_materialize, 1) \
397+
V(Wasm_callFunction, 2)
396398

397399
// List of bootstrap native entry points used in the dart:mirror library.
398400
#define MIRRORS_BOOTSTRAP_NATIVE_LIST(V) \

runtime/vm/object.cc

+12
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,14 @@ RawError* Object::Init(Isolate* isolate,
19151915
pending_classes.Add(cls);
19161916
RegisterClass(cls, Symbols::FfiDynamicLibrary(), lib);
19171917

1918+
lib = Library::LookupLibrary(thread, Symbols::DartWasm());
1919+
if (lib.IsNull()) {
1920+
lib = Library::NewLibraryHelper(Symbols::DartWasm(), true);
1921+
lib.SetLoadRequested();
1922+
lib.Register(thread);
1923+
}
1924+
object_store->set_bootstrap_library(ObjectStore::kWasm, lib);
1925+
19181926
// Finish the initialization by compiling the bootstrap scripts containing
19191927
// the base interfaces and the implementation of the internal classes.
19201928
const Error& error = Error::Handle(
@@ -11698,6 +11706,10 @@ RawLibrary* Library::VMServiceLibrary() {
1169811706
return Isolate::Current()->object_store()->_vmservice_library();
1169911707
}
1170011708

11709+
RawLibrary* Library::WasmLibrary() {
11710+
return Isolate::Current()->object_store()->wasm_library();
11711+
}
11712+
1170111713
const char* Library::ToCString() const {
1170211714
const String& name = String::Handle(url());
1170311715
return OS::SCreate(Thread::Current()->zone(), "Library:'%s'",

runtime/vm/object.h

+1
Original file line numberDiff line numberDiff line change
@@ -4243,6 +4243,7 @@ class Library : public Object {
42434243
static RawLibrary* ProfilerLibrary();
42444244
static RawLibrary* TypedDataLibrary();
42454245
static RawLibrary* VMServiceLibrary();
4246+
static RawLibrary* WasmLibrary();
42464247

42474248
// Eagerly compile all classes and functions in the library.
42484249
static RawError* CompileAll(bool ignore_error = false);

runtime/vm/object_store.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class ObjectPointerVisitor;
2929
M(Mirrors, mirrors) \
3030
M(Profiler, profiler) \
3131
M(TypedData, typed_data) \
32-
M(VMService, _vmservice)
32+
M(VMService, _vmservice) \
33+
M(Wasm, wasm)
3334

3435
#define OBJECT_STORE_FIELD_LIST(R_, RW) \
3536
RW(Class, object_class) \
@@ -101,6 +102,7 @@ class ObjectPointerVisitor;
101102
RW(Library, root_library) \
102103
RW(Library, typed_data_library) \
103104
RW(Library, _vmservice_library) \
105+
RW(Library, wasm_library) \
104106
RW(GrowableObjectArray, libraries) \
105107
RW(Array, libraries_map) \
106108
RW(GrowableObjectArray, closure_functions) \

runtime/vm/symbols.h

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class ObjectPointerVisitor;
7272
V(DartExtensionScheme, "dart-ext:") \
7373
V(DartFfi, "dart:ffi") \
7474
V(DartFfiLibName, "ffi") \
75+
V(DartWasm, "dart:wasm") \
76+
V(DartWasmLibName, "wasm") \
77+
V(DartLibraryWasm, "dart.library.wasm") \
7578
V(DartIOLibName, "dart.io") \
7679
V(DartInternal, "dart:_internal") \
7780
V(DartIsVM, "dart.isVM") \

sdk/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ declare_args() {
8484
# ......math/
8585
# ......mirrors/
8686
# ......typed_data/
87+
# ......wasm/
8788
# ......api_readme.md
8889
# ....model/
8990
# ......lexeme/
@@ -216,6 +217,7 @@ _full_sdk_libraries = [
216217
"profiler",
217218
"svg",
218219
"typed_data",
220+
"wasm",
219221
"web_audio",
220222
"web_gl",
221223
"web_sql",

sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ const Map<String, LibraryInfo> libraries = const {
134134
categories: "Client",
135135
maturity: Maturity.WEB_STABLE,
136136
platforms: DART2JS_PLATFORM),
137+
"wasm": const LibraryInfo("wasm/wasm.dart",
138+
categories: "Server", maturity: Maturity.EXPERIMENTAL),
137139
"web_audio": const LibraryInfo("web_audio/dart2js/web_audio_dart2js.dart",
138140
categories: "Client",
139141
maturity: Maturity.WEB_STABLE,

sdk/lib/libraries.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@
7171
],
7272
"uri": "ffi/ffi.dart"
7373
},
74+
"wasm": {
75+
"patches": [
76+
"../../runtime/lib/wasm_patch.dart"
77+
],
78+
"uri": "wasm/wasm.dart"
79+
},
7480
"typed_data": {
7581
"patches": "../../runtime/lib/typed_data_patch.dart",
7682
"uri": "typed_data/typed_data.dart"
@@ -481,4 +487,4 @@
481487
}
482488
}
483489
}
484-
}
490+
}

sdk/lib/libraries.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ vm:
9494
- "../../runtime/lib/ffi_dynamic_library_patch.dart"
9595
- "../../runtime/lib/ffi_native_type_patch.dart"
9696

97+
wasm:
98+
uri: "wasm/wasm.dart"
99+
patches:
100+
- "../../runtime/lib/wasm_patch.dart"
101+
97102
_http:
98103
uri: "_http/http.dart"
99104

sdk/lib/wasm/wasm.dart

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// {@category VM}
6+
/// {@nodoc}
7+
library dart.wasm;
8+
9+
int callWasm(String name, int arg) {
10+
return _callWasm(name, arg);
11+
}
12+
13+
external int _callWasm(String name, int arg);

sdk/lib/wasm/wasm_sources.gni

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
wasm_sdk_sources = [
6+
"wasm.dart",
7+
]

0 commit comments

Comments
 (0)