소스 검색

LibWeb: Implement OscillatorNode.type

This is a simple getter and setter of the OscillatorType enum, with
error checking to not allow 'custom', as that should only be changed
through 'setPeriodicWave()'.
Shannon Booth 1 년 전
부모
커밋
099c9e4a7e

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

@@ -4,3 +4,7 @@ AudioNode
 EventTarget
 EventTarget
 Object
 Object
 context: '[object OfflineAudioContext], is same as original: true
 context: '[object OfflineAudioContext], is same as original: true
+Error creating node: 'InvalidStateError: Oscillator node type cannot be set to 'custom''
+oscillator node type: 'sine'
+Error: 'InvalidStateError: Oscillator node type cannot be set to 'custom'', type is: sine
+oscillator node type: 'triangle'

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

@@ -15,5 +15,22 @@
 
 
         // Context getter
         // Context getter
         println(`context: '${oscillator.context}, is same as original: ${audioContext === oscillator.context}`);
         println(`context: '${oscillator.context}, is same as original: ${audioContext === oscillator.context}`);
+
+        // Invalid type in constructor
+        try {
+            new OscillatorNode(audioContext, { type: 'custom' });
+        } catch (e) {
+            println(`Error creating node: '${e}'`);
+        }
+
+        // Type attribute
+        println(`oscillator node type: '${oscillator.type}'`);
+        try {
+            oscillator.type = 'custom';
+        } catch (e) {
+            println(`Error: '${e}', type is: ${oscillator.type}`);
+        }
+        oscillator.type = 'triangle';
+        println(`oscillator node type: '${oscillator.type}'`);
     });
     });
 </script>
 </script>

+ 30 - 1
Userland/Libraries/LibWeb/WebAudio/OscillatorNode.cpp

@@ -23,7 +23,9 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> OscillatorNode::create(JS:
 WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> OscillatorNode::construct_impl(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext> context, OscillatorOptions const& options)
 WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> OscillatorNode::construct_impl(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext> context, OscillatorOptions const& options)
 {
 {
     // FIXME: Invoke "Initialize the AudioNode" steps.
     // FIXME: Invoke "Initialize the AudioNode" steps.
-    return realm.vm().heap().allocate<OscillatorNode>(realm, realm, context, options);
+    TRY(verify_valid_type(realm, options.type));
+    auto node = realm.vm().heap().allocate<OscillatorNode>(realm, realm, context, options);
+    return node;
 }
 }
 
 
 OscillatorNode::OscillatorNode(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext> context, OscillatorOptions const&)
 OscillatorNode::OscillatorNode(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext> context, OscillatorOptions const&)
@@ -31,6 +33,33 @@ OscillatorNode::OscillatorNode(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioConte
 {
 {
 }
 }
 
 
+// https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
+Bindings::OscillatorType OscillatorNode::type() const
+{
+    return m_type;
+}
+
+// https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
+WebIDL::ExceptionOr<void> OscillatorNode::verify_valid_type(JS::Realm& realm, Bindings::OscillatorType type)
+{
+    // The shape of the periodic waveform. It may directly be set to any of the type constant values except
+    // for "custom". ⌛ Doing so MUST throw an InvalidStateError exception. The setPeriodicWave() method can
+    // be used to set a custom waveform, which results in this attribute being set to "custom". The default
+    // value is "sine". When this attribute is set, the phase of the oscillator MUST be conserved.
+    if (type == Bindings::OscillatorType::Custom)
+        return WebIDL::InvalidStateError::create(realm, "Oscillator node type cannot be set to 'custom'"_fly_string);
+
+    return {};
+}
+
+// https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
+WebIDL::ExceptionOr<void> OscillatorNode::set_type(Bindings::OscillatorType type)
+{
+    TRY(verify_valid_type(realm(), type));
+    m_type = type;
+    return {};
+}
+
 void OscillatorNode::initialize(JS::Realm& realm)
 void OscillatorNode::initialize(JS::Realm& realm)
 {
 {
     Base::initialize(realm);
     Base::initialize(realm);

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

@@ -30,11 +30,20 @@ public:
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> create(JS::Realm&, JS::NonnullGCPtr<BaseAudioContext>, OscillatorOptions const& = {});
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> create(JS::Realm&, JS::NonnullGCPtr<BaseAudioContext>, OscillatorOptions const& = {});
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> construct_impl(JS::Realm&, JS::NonnullGCPtr<BaseAudioContext>, OscillatorOptions const& = {});
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> construct_impl(JS::Realm&, JS::NonnullGCPtr<BaseAudioContext>, OscillatorOptions const& = {});
 
 
+    Bindings::OscillatorType type() const;
+    WebIDL::ExceptionOr<void> set_type(Bindings::OscillatorType);
+
 protected:
 protected:
     OscillatorNode(JS::Realm&, JS::NonnullGCPtr<BaseAudioContext>, OscillatorOptions const& = {});
     OscillatorNode(JS::Realm&, JS::NonnullGCPtr<BaseAudioContext>, OscillatorOptions const& = {});
 
 
     virtual void initialize(JS::Realm&) override;
     virtual void initialize(JS::Realm&) override;
     virtual void visit_edges(Cell::Visitor&) override;
     virtual void visit_edges(Cell::Visitor&) override;
+
+private:
+    static WebIDL::ExceptionOr<void> verify_valid_type(JS::Realm&, Bindings::OscillatorType);
+
+    // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
+    Bindings::OscillatorType m_type { Bindings::OscillatorType::Sine };
 };
 };
 
 
 }
 }

+ 1 - 1
Userland/Libraries/LibWeb/WebAudio/OscillatorNode.idl

@@ -22,7 +22,7 @@ dictionary OscillatorOptions : AudioNodeOptions {
 [Exposed=Window]
 [Exposed=Window]
 interface OscillatorNode : AudioScheduledSourceNode {
 interface OscillatorNode : AudioScheduledSourceNode {
     constructor(BaseAudioContext context, optional OscillatorOptions options = {});
     constructor(BaseAudioContext context, optional OscillatorOptions options = {});
-    // FIXME: attribute OscillatorType type;
+    attribute OscillatorType type;
     // FIXME: readonly attribute AudioParam frequency;
     // FIXME: readonly attribute AudioParam frequency;
     // FIXME: readonly attribute AudioParam detune;
     // FIXME: readonly attribute AudioParam detune;
     // FIXME: undefined setPeriodicWave(PeriodicWave periodicWave);
     // FIXME: undefined setPeriodicWave(PeriodicWave periodicWave);