summaryrefslogtreecommitdiffstats
path: root/js/src/wasm/WasmAnyRef.cpp
blob: 8e3f716d41836dd5fac584536a137941bb147e8b (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
118
119
120
121
122
123
124
125
126
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 *
 * Copyright 2023 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "wasm/WasmAnyRef.h"

#include "vm/JSContext.h"
#include "vm/JSObject.h"
#include "vm/NativeObject.h"

#include "vm/JSObject-inl.h"

using namespace js;
using namespace js::wasm;

class WasmValueBox : public NativeObject {
 public:
  static const unsigned VALUE_SLOT = 0;
  static const unsigned RESERVED_SLOTS = 1;
  static const JSClass class_;

  static WasmValueBox* create(JSContext* cx, HandleValue value);
  Value value() const { return getFixedSlot(VALUE_SLOT); }
};

const JSClass WasmValueBox::class_ = {
    "WasmValueBox", JSCLASS_HAS_RESERVED_SLOTS(RESERVED_SLOTS)};

WasmValueBox* WasmValueBox::create(JSContext* cx, HandleValue value) {
  WasmValueBox* obj = NewObjectWithGivenProto<WasmValueBox>(cx, nullptr);
  if (!obj) {
    return nullptr;
  }
  obj->setFixedSlot(VALUE_SLOT, value);
  return obj;
}

const JSClass* AnyRef::valueBoxClass() { return &WasmValueBox::class_; }

size_t AnyRef::valueBoxOffsetOfValue() {
  return NativeObject::getFixedSlotOffset(WasmValueBox::VALUE_SLOT);
}

bool AnyRef::fromJSValue(JSContext* cx, HandleValue value,
                         MutableHandleAnyRef result) {
  if (value.isNull()) {
    result.set(AnyRef::null());
    return true;
  }

  if (value.isString()) {
    JSString* string = value.toString();
    result.set(AnyRef::fromJSString(string));
    return true;
  }

  if (value.isObject()) {
    JSObject& obj = value.toObject();
    MOZ_ASSERT(!obj.is<WasmValueBox>());
    MOZ_ASSERT(obj.compartment() == cx->compartment());
    result.set(AnyRef::fromJSObject(obj));
    return true;
  }

  if (value.isInt32() && !int32NeedsBoxing(value.toInt32())) {
    result.set(AnyRef::fromInt32(value.toInt32()));
    return true;
  }

  if (value.isDouble()) {
    double doubleValue = value.toDouble();
    int32_t intValue;
    if (mozilla::NumberIsInt32(doubleValue, &intValue) &&
        !int32NeedsBoxing(intValue)) {
      result.set(AnyRef::fromInt32(intValue));
      return true;
    }
  }

  JSObject* box = AnyRef::boxValue(cx, value);
  if (!box) {
    return false;
  }
  result.set(AnyRef::fromJSObject(*box));
  return true;
}

JSObject* AnyRef::boxValue(JSContext* cx, HandleValue value) {
  MOZ_ASSERT(AnyRef::valueNeedsBoxing(value));
  return WasmValueBox::create(cx, value);
}

Value wasm::AnyRef::toJSValue() const {
  // If toJSValue needs to allocate then we need a more complicated API, and
  // we need to root the value in the callers, see comments in callExport().
  Value value;
  if (isNull()) {
    value.setNull();
  } else if (isJSString()) {
    value.setString(toJSString());
  } else if (isI31()) {
    value.setInt32(toI31());
  } else {
    JSObject& obj = toJSObject();
    if (obj.is<WasmValueBox>()) {
      value = obj.as<WasmValueBox>().value();
    } else {
      value.setObject(obj);
    }
  }
  return value;
}