summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testWasmEncoder.cpp
blob: a90249264ceded9b1b2b9d8b95f9902d1b792a5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jit/MacroAssembler.h"

#include "jsapi-tests/tests.h"
#include "jsapi-tests/testsJit.h"

#include "wasm/WasmConstants.h"
#include "wasm/WasmFeatures.h"  // AnyCompilerAvailable
#include "wasm/WasmGenerator.h"
#include "wasm/WasmSignalHandlers.h"  // EnsureFullSignalHandlers
#include "wasm/WasmValType.h"

using namespace js;
using namespace js::jit;
using namespace js::wasm;

static bool TestTruncFn(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);
  double d = args[0].toDouble();
  args.rval().setInt32((int)d);
  return true;
}

// Check if wasm modules can be encoded in C++ and run.
BEGIN_TEST(testWasmEncodeBasic) {
  if (!AnyCompilerAvailable(cx)) {
    knownFail = true;
    return false;
  }

  EnsureFullSignalHandlers(cx);

  FeatureOptions options;
  ScriptedCaller scriptedCaller;
  SharedCompileArgs compileArgs =
      CompileArgs::buildAndReport(cx, std::move(scriptedCaller), options);

  ModuleEnvironment moduleEnv(compileArgs->features);
  CompilerEnvironment compilerEnv(CompileMode::Once, Tier::Optimized,
                                  DebugEnabled::False);
  compilerEnv.computeParameters();
  MOZ_ALWAYS_TRUE(moduleEnv.init());

  ValTypeVector paramsImp, resultsImp;
  MOZ_ALWAYS_TRUE(paramsImp.emplaceBack(ValType::F64) &&
                  resultsImp.emplaceBack(ValType::I32));

  CacheableName ns;
  CacheableName impName;
  MOZ_ALWAYS_TRUE(CacheableName::fromUTF8Chars("t", &impName));
  MOZ_ALWAYS_TRUE(moduleEnv.addImportedFunc(std::move(paramsImp),
                                            std::move(resultsImp),
                                            std::move(ns), std::move(impName)));

  ValTypeVector params, results;
  MOZ_ALWAYS_TRUE(results.emplaceBack(ValType::I32));
  CacheableName expName;
  MOZ_ALWAYS_TRUE(CacheableName::fromUTF8Chars("r", &expName));
  MOZ_ALWAYS_TRUE(moduleEnv.addDefinedFunc(std::move(params),
                                           std::move(results), true,
                                           mozilla::Some(std::move(expName))));

  ModuleGenerator mg(*compileArgs, &moduleEnv, &compilerEnv, nullptr, nullptr,
                     nullptr);
  MOZ_ALWAYS_TRUE(mg.init(nullptr));

  // Build function and keep bytecode around until the end.
  Bytes bytecode;
  {
    Encoder encoder(bytecode);
    MOZ_ALWAYS_TRUE(EncodeLocalEntries(encoder, ValTypeVector()));
    MOZ_ALWAYS_TRUE(encoder.writeOp(Op::F64Const) &&
                    encoder.writeFixedF64(42.3));
    MOZ_ALWAYS_TRUE(encoder.writeOp(Op::Call) && encoder.writeVarU32(0));
    MOZ_ALWAYS_TRUE(encoder.writeOp(Op::End));
  }
  MOZ_ALWAYS_TRUE(mg.compileFuncDef(1, 0, bytecode.begin(),
                                    bytecode.begin() + bytecode.length()));
  MOZ_ALWAYS_TRUE(mg.finishFuncDefs());

  SharedBytes shareableBytes = js_new<ShareableBytes>();
  MOZ_ALWAYS_TRUE(shareableBytes);
  SharedModule module = mg.finishModule(*shareableBytes);
  MOZ_ALWAYS_TRUE(module);

  MOZ_ASSERT(module->imports().length() == 1);
  MOZ_ASSERT(module->exports().length() == 1);

  // Instantiate and run.
  {
    Rooted<ImportValues> imports(cx);
    RootedFunction func(cx, NewNativeFunction(cx, TestTruncFn, 0, nullptr));
    MOZ_ALWAYS_TRUE(func);
    MOZ_ALWAYS_TRUE(imports.get().funcs.append(func));

    Rooted<WasmInstanceObject*> instance(cx);
    MOZ_ALWAYS_TRUE(module->instantiate(cx, imports.get(), nullptr, &instance));
    RootedFunction wasmFunc(cx);
    MOZ_ALWAYS_TRUE(
        WasmInstanceObject::getExportedFunction(cx, instance, 1, &wasmFunc));

    JSAutoRealm ar(cx, wasmFunc);
    RootedValue rval(cx);
    RootedValue fval(cx);
    MOZ_ALWAYS_TRUE(
        JS::Call(cx, fval, wasmFunc, JS::HandleValueArray::empty(), &rval));
    MOZ_RELEASE_ASSERT(rval.toInt32() == 42);
  }
  return true;
}
END_TEST(testWasmEncodeBasic)