Kaynağa Gözat

LibWeb: Add constructor for OscillatorNode

This is still missing a bunch of spec steps to construct the
audio node based on the parameters of the OscillatorNode, but it is at
least enough to construct an object to be able to add a basic test which
can get built upon as more is implemented.
Shannon Booth 1 yıl önce
ebeveyn
işleme
97576d27b9

+ 5 - 0
Tests/LibWeb/Text/expected/WebAudio/OscillatorNode.txt

@@ -0,0 +1,5 @@
+OscillatorNode
+AudioScheduledSourceNode
+AudioNode
+EventTarget
+Object

+ 15 - 0
Tests/LibWeb/Text/input/WebAudio/OscillatorNode.html

@@ -0,0 +1,15 @@
+<script src="../include.js"></script>
+<script>
+    test(() => {
+        const audioContext = new OfflineAudioContext(1, 5000, 44100);
+
+        const oscillator = audioContext.createOscillator();
+
+        let prototype = Object.getPrototypeOf(oscillator);
+
+        while (prototype) {
+            println(prototype.constructor.name);
+            prototype = Object.getPrototypeOf(prototype);
+        }
+    });
+</script>

+ 9 - 0
Userland/Libraries/LibWeb/WebAudio/AudioNode.cpp

@@ -6,11 +6,19 @@
 
 #include <LibWeb/Bindings/Intrinsics.h>
 #include <LibWeb/WebAudio/AudioNode.h>
+#include <LibWeb/WebAudio/BaseAudioContext.h>
 
 namespace Web::WebAudio {
 
 JS_DEFINE_ALLOCATOR(AudioNode);
 
+AudioNode::AudioNode(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext> context)
+    : DOM::EventTarget(realm)
+    , m_context(context)
+
+{
+}
+
 AudioNode::~AudioNode() = default;
 
 // https://webaudio.github.io/web-audio-api/#dom-audionode-connect
@@ -91,6 +99,7 @@ void AudioNode::initialize(JS::Realm& realm)
 void AudioNode::visit_edges(Cell::Visitor& visitor)
 {
     Base::visit_edges(visitor);
+    visitor.visit(m_context);
 }
 
 }

+ 5 - 0
Userland/Libraries/LibWeb/WebAudio/AudioNode.h

@@ -42,8 +42,13 @@ public:
     void disconnect(JS::NonnullGCPtr<AudioParam> destination_param, WebIDL::UnsignedLong output);
 
 protected:
+    AudioNode(JS::Realm&, JS::NonnullGCPtr<BaseAudioContext>);
+
     virtual void initialize(JS::Realm&) override;
     virtual void visit_edges(Cell::Visitor&) override;
+
+private:
+    JS::NonnullGCPtr<BaseAudioContext> m_context;
 };
 
 }

+ 5 - 0
Userland/Libraries/LibWeb/WebAudio/AudioScheduledSourceNode.cpp

@@ -13,6 +13,11 @@ namespace Web::WebAudio {
 
 JS_DEFINE_ALLOCATOR(AudioScheduledSourceNode);
 
+AudioScheduledSourceNode::AudioScheduledSourceNode(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext> context)
+    : AudioNode(realm, context)
+{
+}
+
 AudioScheduledSourceNode::~AudioScheduledSourceNode() = default;
 
 // https://webaudio.github.io/web-audio-api/#dom-audioscheduledsourcenode-onended

+ 2 - 0
Userland/Libraries/LibWeb/WebAudio/AudioScheduledSourceNode.h

@@ -25,6 +25,8 @@ public:
     WebIDL::ExceptionOr<void> stop(double when = 0);
 
 protected:
+    AudioScheduledSourceNode(JS::Realm&, JS::NonnullGCPtr<BaseAudioContext>);
+
     virtual void initialize(JS::Realm&) override;
     virtual void visit_edges(Cell::Visitor&) override;
 };

+ 8 - 2
Userland/Libraries/LibWeb/WebAudio/OscillatorNode.cpp

@@ -20,9 +20,15 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> OscillatorNode::create(JS:
 }
 
 // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-oscillatornode
-WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> OscillatorNode::construct_impl(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext>, OscillatorOptions const&)
+WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> OscillatorNode::construct_impl(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext> context, OscillatorOptions const& options)
+{
+    // FIXME: Invoke "Initialize the AudioNode" steps.
+    return realm.vm().heap().allocate<OscillatorNode>(realm, realm, context, options);
+}
+
+OscillatorNode::OscillatorNode(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext> context, OscillatorOptions const&)
+    : AudioScheduledSourceNode(realm, context)
 {
-    return WebIDL::NotSupportedError::create(realm, "FIXME: Implement OscillatorNode::construct_impl"_fly_string);
 }
 
 void OscillatorNode::initialize(JS::Realm& realm)

+ 2 - 0
Userland/Libraries/LibWeb/WebAudio/OscillatorNode.h

@@ -31,6 +31,8 @@ public:
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> construct_impl(JS::Realm&, JS::NonnullGCPtr<BaseAudioContext>, OscillatorOptions const& = {});
 
 protected:
+    OscillatorNode(JS::Realm&, JS::NonnullGCPtr<BaseAudioContext>, OscillatorOptions const& = {});
+
     virtual void initialize(JS::Realm&) override;
     virtual void visit_edges(Cell::Visitor&) override;
 };