52 VM& vm = exec->vm();
53 auto throwScope = DECLARE_THROW_SCOPE(vm);
54 if (exec->argumentCount() != 1)
55 return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("WebAssembly.Memory expects exactly one argument"))));
56
57 auto getUint32 = [&] (JSValue value) -> uint32_t {
58 double doubleValue = value.toInteger(exec);
59 RETURN_IF_EXCEPTION(throwScope, { });
60 if (doubleValue < 0 || doubleValue > UINT_MAX) {
61 throwException(exec, throwScope,
62 createTypeError(exec, ASCIILiteral("WebAssembly.Memory expects the 'initial' property to be an integer in the range: [0, 2^32 - 1]")));
63 return 0;
64 }
65 return static_cast<uint32_t>(doubleValue);
66 };
67
68 JSObject* memoryDescriptor;
69 {
70 JSValue argument = exec->argument(0);
71 if (!argument.isObject())
72 return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("WebAssembly.Memory excepts its first argument to be an object"))));
73 memoryDescriptor = jsCast<JSObject*>(argument);
74 }
75
76 Wasm::PageCount minSize;
77 {
78 Identifier initial = Identifier::fromString(&vm, "initial");
79 JSValue minSizeValue = memoryDescriptor->get(exec, initial);
80 RETURN_IF_EXCEPTION(throwScope, { });
81 uint32_t size = getUint32(minSizeValue);
82 RETURN_IF_EXCEPTION(throwScope, { });
83 if (!Wasm::PageCount::isValid(size))
84 return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("WebAssembly.Memory invalid 'initial' page count"))));
85 minSize = Wasm::PageCount(size);
86 }
87
88 Wasm::PageCount maxSize;
89 {
90 Identifier maximum = Identifier::fromString(&vm, "maximum");
91 bool hasProperty = memoryDescriptor->hasProperty(exec, maximum);
92 RETURN_IF_EXCEPTION(throwScope, { });
93 if (hasProperty) {
94 JSValue maxSizeValue = memoryDescriptor->get(exec, maximum);
95 RETURN_IF_EXCEPTION(throwScope, { });
96 uint32_t size = getUint32(maxSizeValue);
97 if (!Wasm::PageCount::isValid(size))
98 return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("WebAssembly.Memory invalid 'initial' page count"))));
99 RETURN_IF_EXCEPTION(throwScope, { });
100 maxSize = Wasm::PageCount(size);
101
102 if (minSize > maxSize) {
103 return JSValue::encode(throwException(exec, throwScope,
104 createTypeError(exec, ASCIILiteral("WebAssembly.Memory provided a 'maximum' page count that smaller than the 'initial' page count"))));
105 }
106 } else {
107 // FIXME: what does the spec say to do here.
108 maxSize = minSize;
109 }
110 }
111
112 std::unique_ptr<Wasm::Memory> memory = std::make_unique<Wasm::Memory>(minSize, maxSize);
113
114 return JSValue::encode(JSWebAssemblyMemory::create(vm, exec->lexicalGlobalObject()->WebAssemblyMemoryStructure(), WTFMove(memory)));