|
1 | 1 | /* |
2 | | - * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * The Universal Permissive License (UPL), Version 1.0 |
|
42 | 42 |
|
43 | 43 | import java.nio.ByteBuffer; |
44 | 44 |
|
| 45 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
45 | 46 | import com.oracle.truffle.api.interop.InteropLibrary; |
| 47 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
46 | 48 | import com.oracle.truffle.api.object.Shape; |
47 | 49 | import com.oracle.truffle.api.strings.TruffleString; |
48 | 50 | import com.oracle.truffle.js.runtime.Errors; |
@@ -74,22 +76,38 @@ public boolean isShared() { |
74 | 76 | return shared; |
75 | 77 | } |
76 | 78 |
|
77 | | - public JSArrayBufferObject getBufferObject(JSContext context, JSRealm realm) { |
78 | | - if (bufferObject == null) { |
79 | | - if (!shared) { |
80 | | - bufferObject = JSArrayBuffer.createInteropArrayBuffer(context, realm, wasmMemory); |
81 | | - } else { |
82 | | - synchronized (wasmMemory) { |
83 | | - InteropLibrary lib = InteropLibrary.getUncached(); |
84 | | - ByteBuffer buffer = JSInteropUtil.foreignInteropBufferAsByteBuffer(wasmMemory, lib, realm); |
85 | | - bufferObject = JSSharedArrayBuffer.createSharedArrayBuffer(context, realm, buffer); |
86 | | - boolean status = bufferObject.setIntegrityLevel(true, false); |
87 | | - if (!status) { |
88 | | - throw Errors.createTypeError("Failed to set integrity level of buffer object"); |
89 | | - } |
| 79 | + @TruffleBoundary |
| 80 | + private JSArrayBufferObject createBufferObject(JSContext context, JSRealm realm) { |
| 81 | + InteropLibrary lib = InteropLibrary.getUncached(); |
| 82 | + try { |
| 83 | + if (lib.getBufferSize(wasmMemory) > Integer.MAX_VALUE) { |
| 84 | + throw Errors.createRangeErrorInvalidBufferSize(); |
| 85 | + } |
| 86 | + } catch (UnsupportedMessageException e) { |
| 87 | + throw Errors.createTypeErrorInteropException(wasmMemory, e, "WebAssembly.Memory underlying buffer object is not an interop buffer", null); |
| 88 | + } |
| 89 | + if (!shared) { |
| 90 | + return JSArrayBuffer.createInteropArrayBuffer(context, realm, wasmMemory); |
| 91 | + } else { |
| 92 | + synchronized (wasmMemory) { |
| 93 | + ByteBuffer buffer = JSInteropUtil.foreignInteropBufferAsByteBuffer(wasmMemory, lib, realm); |
| 94 | + if (buffer == null) { |
| 95 | + throw Errors.createTypeError("No ByteBuffer exposed from WebAssembly memory"); |
| 96 | + } |
| 97 | + JSArrayBufferObject bufferObj = JSSharedArrayBuffer.createSharedArrayBuffer(context, realm, buffer); |
| 98 | + boolean status = bufferObj.setIntegrityLevel(true, false); |
| 99 | + if (!status) { |
| 100 | + throw Errors.createTypeError("Failed to set integrity level of buffer object"); |
90 | 101 | } |
| 102 | + return bufferObj; |
91 | 103 | } |
92 | 104 | } |
| 105 | + } |
| 106 | + |
| 107 | + public JSArrayBufferObject getBufferObject(JSContext context, JSRealm realm) { |
| 108 | + if (bufferObject == null) { |
| 109 | + bufferObject = createBufferObject(context, realm); |
| 110 | + } |
93 | 111 | return bufferObject; |
94 | 112 | } |
95 | 113 |
|
|
0 commit comments