JSAudioContextCustom.cpp [plain text]
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "AudioContext.h"
#include "AudioBuffer.h"
#include "Document.h"
#include "JSAudioBuffer.h"
#include "JSAudioContext.h"
#include "JSDOMBinding.h"
#include "JSDOMPromise.h"
#include "JSOfflineAudioContext.h"
#include "OfflineAudioContext.h"
#include <runtime/ArrayBuffer.h>
#include <runtime/Error.h>
#include <runtime/JSArrayBuffer.h>
using namespace JSC;
namespace WebCore {
EncodedJSValue JSC_HOST_CALL constructJSAudioContext(ExecState* exec)
{
DOMConstructorObject* jsConstructor = jsCast<DOMConstructorObject*>(exec->callee());
if (!jsConstructor)
return throwVMError(exec, createReferenceError(exec, "AudioContext constructor callee is unavailable"));
ScriptExecutionContext* scriptExecutionContext = jsConstructor->scriptExecutionContext();
if (!scriptExecutionContext)
return throwVMError(exec, createReferenceError(exec, "AudioContext constructor script execution context is unavailable"));
if (!is<Document>(*scriptExecutionContext))
return throwVMError(exec, createReferenceError(exec, "AudioContext constructor called in a script execution context which is not a document"));
Document& document = downcast<Document>(*scriptExecutionContext);
RefPtr<AudioContext> audioContext;
if (!exec->argumentCount()) {
ExceptionCode ec = 0;
audioContext = AudioContext::create(document, ec);
if (ec) {
setDOMException(exec, ec);
return JSValue::encode(JSValue());
}
if (!audioContext.get())
return throwVMError(exec, createSyntaxError(exec, "audio resources unavailable for AudioContext construction"));
} else {
#if ENABLE(LEGACY_WEB_AUDIO)
document.addConsoleMessage(MessageSource::JS, MessageLevel::Warning, ASCIILiteral("Deprecated AudioContext constructor: use OfflineAudioContext instead"));
if (exec->argumentCount() < 3)
return throwVMError(exec, createNotEnoughArgumentsError(exec));
int32_t numberOfChannels = exec->argument(0).toInt32(exec);
int32_t numberOfFrames = exec->argument(1).toInt32(exec);
float sampleRate = exec->argument(2).toFloat(exec);
if (numberOfChannels <= 0 || numberOfChannels > 10)
return throwVMError(exec, createSyntaxError(exec, "Invalid number of channels"));
if (numberOfFrames <= 0)
return throwVMError(exec, createSyntaxError(exec, "Invalid number of frames"));
if (sampleRate <= 0)
return throwVMError(exec, createSyntaxError(exec, "Invalid sample rate"));
ExceptionCode ec = 0;
audioContext = OfflineAudioContext::create(document, numberOfChannels, numberOfFrames, sampleRate, ec);
if (ec) {
setDOMException(exec, ec);
return throwVMError(exec, createSyntaxError(exec, "Error creating OfflineAudioContext"));
}
#else
return throwVMError(exec, createSyntaxError(exec, "Illegal AudioContext constructor"));
#endif
}
if (!audioContext.get())
return throwVMError(exec, createReferenceError(exec, "Error creating AudioContext"));
return JSValue::encode(CREATE_DOM_WRAPPER(jsConstructor->globalObject(), AudioContext, audioContext.get()));
}
}
#endif // ENABLE(WEB_AUDIO)