forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrule.rs
More file actions
59 lines (50 loc) · 2.04 KB
/
rule.rs
File metadata and controls
59 lines (50 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use napi::{CallContext, JsString, JsObject, JsUndefined, JsFunction, Property, Ref, Result, Env};
use std::cell::RefCell;
use parcel_css::rules::CssRule;
use std::convert::TryInto;
use crate::style_rule;
static mut CLASS: RefCell<Option<Ref<()>>> = RefCell::new(None);
#[js_function(0)]
fn constructor(ctx: CallContext) -> Result<JsUndefined> {
ctx.env.get_undefined()
}
#[js_function(0)]
fn get_css_text(ctx: CallContext) -> Result<JsString> {
let this: JsObject = ctx.this_unchecked();
let rule: &mut CssRule = ctx.env.unwrap(&this)?;
let text = rule.to_css_string();
ctx.env.create_string_from_std(text)
}
pub fn init(exports: &mut JsObject, env: Env) -> Result<()> {
let stylesheet_class = env
.define_class("CSSRule", constructor, &[
Property::new(&env, "cssText")?.with_getter(get_css_text)
])?;
let mut c = unsafe { CLASS.borrow_mut() };
*c = Some(env.create_reference(&stylesheet_class)?);
exports.set_named_property("CSSRule", stylesheet_class)?;
Ok(())
}
pub fn inherit(env: &Env, obj: JsFunction) -> Result<JsFunction> {
let r = unsafe { CLASS.borrow() };
let r = r.as_ref().unwrap();
let super_class: JsFunction = env.get_reference_value(&r)?;
let super_class_obj: JsObject = super_class.coerce_to_object()?;
let class_obj: JsObject = obj.coerce_to_object()?;
// Based on https://github.com/nodejs/node-addon-api/issues/229#issuecomment-383583352
let global = env.get_global()?;
let object: JsFunction = global.get_named_property("Object")?;
let object = object.coerce_to_object()?;
let set_proto: JsFunction = object.get_named_property("setPrototypeOf")?;
let proto: JsObject = class_obj.get_named_property("prototype")?;
let super_proto: JsObject = super_class_obj.get_named_property("prototype")?;
set_proto.call(None, &[&proto, &super_proto])?;
set_proto.call(None, &[&class_obj, &super_class_obj])?;
class_obj.into_unknown().try_into()
}
pub fn create(env: &Env, rule: CssRule) -> Result<JsObject> {
match rule {
CssRule::Style(_) => style_rule::create(env, rule),
_ => unimplemented!()
}
}