fix(napi): handle the referenced object is finalized before Reference::drop

This commit is contained in:
Devon Govett 2022-05-02 20:27:18 -04:00 committed by LongYinan
parent 68a6d507ff
commit 91c62c4616
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
7 changed files with 90 additions and 21 deletions

View file

@ -279,6 +279,11 @@ Generated by [AVA](https://avajs.dev).
export class CssStyleSheet {␊
constructor(rules: Array<string>)␊
get rules(): CssRuleList␊
anotherCssStyleSheet(): AnotherCssStyleSheet␊
}␊
export type AnotherCSSStyleSheet = AnotherCssStyleSheet␊
export class AnotherCssStyleSheet {␊
get rules(): CssRuleList␊
}␊
export namespace xxh3 {␊
export const ALIGNMENT: number␊

View file

@ -205,6 +205,8 @@ test('should be able to into_reference', (t) => {
const sheet = new CssStyleSheet(rules)
t.is(sheet.rules, sheet.rules)
t.deepEqual(sheet.rules.getRules(), rules)
const anotherStyleSheet = sheet.anotherCssStyleSheet()
t.is(anotherStyleSheet.rules, sheet.rules)
})
test('callback', (t) => {

View file

@ -269,6 +269,11 @@ export type CSSStyleSheet = CssStyleSheet
export class CssStyleSheet {
constructor(rules: Array<string>)
get rules(): CssRuleList
anotherCssStyleSheet(): AnotherCssStyleSheet
}
export type AnotherCSSStyleSheet = AnotherCssStyleSheet
export class AnotherCssStyleSheet {
get rules(): CssRuleList
}
export namespace xxh3 {
export const ALIGNMENT: number

View file

@ -76,26 +76,54 @@ impl CSSRuleList {
#[napi]
pub struct CSSStyleSheet {
inner: Rc<RefCell<OwnedStyleSheet>>,
rules: Option<Reference<CSSRuleList>>,
}
#[napi]
pub struct AnotherCSSStyleSheet {
inner: Rc<RefCell<OwnedStyleSheet>>,
rules: Reference<CSSRuleList>,
}
#[napi]
impl CSSStyleSheet {
#[napi(constructor)]
pub fn new(env: Env, rules: Vec<String>) -> Result<Self> {
let inner = Rc::new(RefCell::new(OwnedStyleSheet { rules }));
let rules = CSSRuleList::into_reference(
CSSRuleList {
owned: inner.clone(),
},
env,
)?;
Ok(CSSStyleSheet { inner, rules })
}
impl AnotherCSSStyleSheet {
#[napi(getter)]
pub fn rules(&self, env: Env) -> Result<Reference<CSSRuleList>> {
self.rules.clone(env)
}
}
#[napi]
impl CSSStyleSheet {
#[napi(constructor)]
pub fn new(rules: Vec<String>) -> Result<Self> {
let inner = Rc::new(RefCell::new(OwnedStyleSheet { rules }));
Ok(CSSStyleSheet { inner, rules: None })
}
#[napi(getter)]
pub fn rules(&mut self, env: Env) -> Result<Reference<CSSRuleList>> {
if let Some(rules) = &self.rules {
return rules.clone(env);
}
let rules = CSSRuleList::into_reference(
CSSRuleList {
owned: self.inner.clone(),
},
env,
)?;
self.rules = Some(rules.clone(env)?);
Ok(rules)
}
#[napi]
pub fn another_css_style_sheet(&self, env: Env) -> Result<AnotherCSSStyleSheet> {
Ok(AnotherCSSStyleSheet {
inner: self.inner.clone(),
rules: self.rules.as_ref().unwrap().clone(env)?,
})
}
}