I am still in the mock phase for a Webaudio plugin. In WebAudio there are many AudioNode subclasses. I see many examples of calling functions, but not any places getting or setting properties.
Example, there is an AnalyserNode. It has properties, AnalyserNode - Web APIs | MDN : fftSize, frequencyBitCount (getter only), minDecibels, maxDecibels, & smoothingTimeConstant.
If it is not a function, how do you define it in c++?
1 Like
Cedric
April 8, 2021, 2:49pm
2
Many ways to do it.
For simple values, you can use Napi instanceValue:
Napi::Function func = DefineClass(
env,
JS_CLASS_NAME,
{
InstanceMethod("isSessionSupported", &XR::IsSessionSupported),
InstanceMethod("requestSession", &XR::RequestSession),
InstanceMethod("getWebXRRenderTarget", &XR::GetWebXRRenderTarget),
InstanceMethod("getNativeRenderTargetProvider", &XR::GetNativeRenderTargetProvider),
InstanceAccessor("nativeXrContext", &XR::GetNativeXrContext, nullptr),
InstanceAccessor("nativeXrContextType", &XR::GetNativeXrContextType, nullptr),
InstanceValue(JS_NATIVE_NAME, Napi::Value::From(env, true)),
});
Napi::Object global = env.Global();
Napi::Object navigator;
if (global.Has(JS_NAVIGATOR_NAME))
{
navigator = global.Get(JS_NAVIGATOR_NAME).As<Napi::Object>();
}
else
{
For values coming from C++, use instanceAccessor:
Napi::HandleScope scope{env};
Napi::Function func = DefineClass(
env,
JS_CLASS_NAME,
{
InstanceMethod("isSessionSupported", &XR::IsSessionSupported),
InstanceMethod("requestSession", &XR::RequestSession),
InstanceMethod("getWebXRRenderTarget", &XR::GetWebXRRenderTarget),
InstanceMethod("getNativeRenderTargetProvider", &XR::GetNativeRenderTargetProvider),
InstanceAccessor("nativeXrContext", &XR::GetNativeXrContext, nullptr),
InstanceAccessor("nativeXrContextType", &XR::GetNativeXrContextType, nullptr),
InstanceValue(JS_NATIVE_NAME, Napi::Value::From(env, true)),
});
Napi::Object global = env.Global();
Napi::Object navigator;
if (global.Has(JS_NAVIGATOR_NAME))
{
navigator = global.Get(JS_NAVIGATOR_NAME).As<Napi::Object>();
}
if the setter is nullptr
then it’s a readonly value.
Also, you can find implementation ‘almost’ everywhere in the repo for Setter/Getter. Like this one:
Napi::Value::From(Env(), deviceSlot),
Napi::Value::From(Env(), inputIndex),
previousState ? Napi::Value::From(Env(), *previousState) : Env().Null(),
currentState ? Napi::Value::From(Env(), *currentState) : Env().Null()
});
}
})}
{
}
Napi::Value NativeInput::Impl::DeviceInputSystem::GetOnDeviceConnected(const Napi::CallbackInfo&)
{
return m_onDeviceConnected.Value();
}
void NativeInput::Impl::DeviceInputSystem::SetOnDeviceConnected(const Napi::CallbackInfo&, const Napi::Value& value)
{
m_onDeviceConnected = Napi::Persistent(value.As<Napi::Function>());
}
Napi::Value NativeInput::Impl::DeviceInputSystem::GetOnDeviceDisconnected(const Napi::CallbackInfo&)
}
})}
{
}
Napi::Value NativeInput::Impl::DeviceInputSystem::GetOnDeviceConnected(const Napi::CallbackInfo&)
{
return m_onDeviceConnected.Value();
}
void NativeInput::Impl::DeviceInputSystem::SetOnDeviceConnected(const Napi::CallbackInfo&, const Napi::Value& value)
{
m_onDeviceConnected = Napi::Persistent(value.As<Napi::Function>());
}
Napi::Value NativeInput::Impl::DeviceInputSystem::GetOnDeviceDisconnected(const Napi::CallbackInfo&)
{
return m_onDeviceDisconnected.Value();
}
void NativeInput::Impl::DeviceInputSystem::SetOnDeviceDisconnected(const Napi::CallbackInfo&, const Napi::Value& value)
1 Like
This is documented here as well, though some of it is node-specific which can be ignored.
nodejs/node-addon-api: Module for using N-API from C++ (github.com)
1 Like