Serenity Operating System
1/*
2 * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
3 * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <AK/ByteBuffer.h>
9#include <AK/NumericLimits.h>
10#include <LibJS/Runtime/AbstractOperations.h>
11#include <LibJS/Runtime/ArrayBuffer.h>
12#include <LibJS/Runtime/DataView.h>
13#include <LibJS/Runtime/PropertyKey.h>
14#include <LibJS/Runtime/TypedArray.h>
15#include <LibWeb/WebIDL/AbstractOperations.h>
16
17namespace Web::WebIDL {
18
19// https://webidl.spec.whatwg.org/#dfn-get-buffer-source-copy
20ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source)
21{
22 // 1. Let esBufferSource be the result of converting bufferSource to an ECMAScript value.
23
24 // 2. Let esArrayBuffer be esBufferSource.
25 JS::ArrayBuffer* es_array_buffer;
26
27 // 3. Let offset be 0.
28 u32 offset = 0;
29
30 // 4. Let length be 0.
31 u32 length = 0;
32
33 // 5. If esBufferSource has a [[ViewedArrayBuffer]] internal slot, then:
34 if (is<JS::TypedArrayBase>(buffer_source)) {
35 auto const& es_buffer_source = static_cast<JS::TypedArrayBase const&>(buffer_source);
36
37 // 1. Set esArrayBuffer to esBufferSource.[[ViewedArrayBuffer]].
38 es_array_buffer = es_buffer_source.viewed_array_buffer();
39
40 // 2. Set offset to esBufferSource.[[ByteOffset]].
41 offset = es_buffer_source.byte_offset();
42
43 // 3. Set length to esBufferSource.[[ByteLength]].
44 length = es_buffer_source.byte_length();
45 } else if (is<JS::DataView>(buffer_source)) {
46 auto const& es_buffer_source = static_cast<JS::DataView const&>(buffer_source);
47
48 // 1. Set esArrayBuffer to esBufferSource.[[ViewedArrayBuffer]].
49 es_array_buffer = es_buffer_source.viewed_array_buffer();
50
51 // 2. Set offset to esBufferSource.[[ByteOffset]].
52 offset = es_buffer_source.byte_offset();
53
54 // 3. Set length to esBufferSource.[[ByteLength]].
55 length = es_buffer_source.byte_length();
56 }
57 // 6. Otherwise:
58 else {
59 // 1. Assert: esBufferSource is an ArrayBuffer or SharedArrayBuffer object.
60 auto const& es_buffer_source = static_cast<JS::ArrayBuffer const&>(buffer_source);
61 es_array_buffer = &const_cast<JS ::ArrayBuffer&>(es_buffer_source);
62
63 // 2. Set length to esBufferSource.[[ArrayBufferByteLength]].
64 length = es_buffer_source.byte_length();
65 }
66
67 // 7. If ! IsDetachedBuffer(esArrayBuffer) is true, then return the empty byte sequence.
68 if (es_array_buffer->is_detached())
69 return ByteBuffer {};
70
71 // 8. Let bytes be a new byte sequence of length equal to length.
72 auto bytes = TRY(ByteBuffer::create_zeroed(length));
73
74 // 9. For i in the range offset to offset + length − 1, inclusive, set bytes[i − offset] to ! GetValueFromBuffer(esArrayBuffer, i, Uint8, true, Unordered).
75 for (u64 i = offset; i < offset + length; ++i) {
76 auto value = es_array_buffer->get_value<u8>(i, true, JS::ArrayBuffer::Unordered);
77 bytes[i - offset] = static_cast<u8>(value.as_double());
78 }
79
80 // 10. Return bytes.
81 return bytes;
82}
83
84// https://webidl.spec.whatwg.org/#invoke-a-callback-function
85JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Value> this_argument, JS::MarkedVector<JS::Value> args)
86{
87 // 1. Let completion be an uninitialized variable.
88 JS::Completion completion;
89
90 // 2. If thisArg was not given, let thisArg be undefined.
91 if (!this_argument.has_value())
92 this_argument = JS::js_undefined();
93
94 // 3. Let F be the ECMAScript object corresponding to callable.
95 auto& function_object = callback.callback;
96
97 // 4. If ! IsCallable(F) is false:
98 if (!function_object.is_function()) {
99 // 1. Note: This is only possible when the callback function came from an attribute marked with [LegacyTreatNonObjectAsNull].
100
101 // 2. Return the result of converting undefined to the callback function’s return type.
102 // FIXME: This does no conversion.
103 return { JS::js_undefined() };
104 }
105
106 // 5. Let realm be F’s associated Realm.
107 // See the comment about associated realm on step 4 of call_user_object_operation.
108 auto& realm = function_object.shape().realm();
109
110 // 6. Let relevant settings be realm’s settings object.
111 auto& relevant_settings = Bindings::host_defined_environment_settings_object(realm);
112
113 // 7. Let stored settings be value’s callback context.
114 auto& stored_settings = callback.callback_context;
115
116 // 8. Prepare to run script with relevant settings.
117 relevant_settings.prepare_to_run_script();
118
119 // 9. Prepare to run a callback with stored settings.
120 stored_settings.prepare_to_run_callback();
121
122 // FIXME: 10. Let esArgs be the result of converting args to an ECMAScript arguments list. If this throws an exception, set completion to the completion value representing the thrown exception and jump to the step labeled return.
123 // For simplicity, we currently make the caller do this. However, this means we can't throw exceptions at this point like the spec wants us to.
124
125 // 11. Let callResult be Call(F, thisArg, esArgs).
126 auto& vm = function_object.vm();
127 auto call_result = JS::call(vm, verify_cast<JS::FunctionObject>(function_object), this_argument.value(), move(args));
128
129 // 12. If callResult is an abrupt completion, set completion to callResult and jump to the step labeled return.
130 if (call_result.is_throw_completion()) {
131 completion = call_result.throw_completion();
132 return clean_up_on_return(stored_settings, relevant_settings, completion);
133 }
134
135 // 13. Set completion to the result of converting callResult.[[Value]] to an IDL value of the same type as the operation’s return type.
136 // FIXME: This does no conversion.
137 completion = call_result.value();
138
139 return clean_up_on_return(stored_settings, relevant_settings, completion);
140}
141
142}