This commit is contained in:
Andrew Morris
2023-08-14 15:42:43 +10:00
parent cf14a21fe9
commit 5abd1c472e
3 changed files with 111 additions and 10 deletions

View File

@@ -88,6 +88,7 @@ impl std::fmt::Display for Definition {
#[derive(Debug, Clone)]
pub enum DefinitionContent {
Function(Function),
FnMeta(FnMeta),
Value(Value),
Lazy(Lazy),
}
@@ -98,6 +99,9 @@ impl std::fmt::Display for DefinitionContent {
DefinitionContent::Function(function) => {
write!(f, "{}", function)
}
DefinitionContent::FnMeta(fn_meta) => {
write!(f, "{}", fn_meta)
}
DefinitionContent::Value(value) => {
write!(f, "{}", value)
}
@@ -108,6 +112,63 @@ impl std::fmt::Display for DefinitionContent {
}
}
#[derive(Debug, Clone)]
pub struct FnMeta {
name: String,
content_hashable: ContentHashable,
}
impl std::fmt::Display for FnMeta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "fn_meta {{\n")?;
write!(
f,
" name: {}",
serde_json::to_string(&self.name).expect("Failed json serialization")
)?;
match &self.content_hashable {
ContentHashable::Empty => {}
ContentHashable::Src(src_hash, deps) => {
write!(f, " srcHash: 0x")?;
for b in src_hash {
write!(f, "{:02x}", b)?;
}
write!(f, ",\n")?;
write!(
f,
" deps: {},\n",
Array {
values: deps.clone()
}
)?;
}
ContentHashable::Hash(content_hash) => {
write!(f, " contentHash: 0x")?;
for b in content_hash {
write!(f, "{:02x}", b)?;
}
write!(f, ",\n")?;
}
}
write!(f, "}}")
}
}
#[derive(Debug, Clone)]
enum ContentHashable {
Empty,
Src([u8; 32], Vec<Value>),
Hash([u8; 32]),
}
#[derive(Hash, PartialEq, Eq, Clone, Debug, PartialOrd, Ord)]
pub struct Pointer {
pub name: String,
@@ -122,7 +183,7 @@ impl std::fmt::Display for Pointer {
#[derive(Default, Debug, Clone)]
pub struct Function {
pub is_generator: bool,
pub metadata: Value,
pub metadata: Option<Pointer>,
pub parameters: Vec<Register>,
pub body: Vec<FnLine>,
}
@@ -130,8 +191,8 @@ pub struct Function {
impl std::fmt::Display for Function {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let metadata_str = match &self.metadata {
Value::Void => "".to_string(),
metadata => format!(" {}", metadata),
None => "".to_string(),
Some(p) => format!(" {}", p),
};
match self.is_generator {

View File

@@ -5,8 +5,8 @@ use std::mem::take;
use swc_common::Spanned;
use crate::asm::{
Builtin, Definition, DefinitionContent, FnLine, Function, Instruction, Label, Object, Pointer,
Register, Value,
Array, Builtin, Definition, DefinitionContent, FnLine, Function, Instruction, Label, Object,
Pointer, Register, Value,
};
use crate::diagnostic::{Diagnostic, DiagnosticContainer, DiagnosticReporter};
use crate::expression_compiler::CompiledExpression;
@@ -34,7 +34,7 @@ impl Functionish {
}
}
pub fn metadata(&self, source: &str) -> Value {
pub fn metadata(&self, mc: &ModuleCompiler) -> Value {
match self {
Functionish::Fn(ident, fn_) => Value::Object(Box::new(Object {
properties: vec![
@@ -48,7 +48,13 @@ impl Functionish {
),
(
Value::String("srcHash".to_string()),
src_hash_asm(source, fn_.span),
src_hash_asm(&mc.source, fn_.span),
),
(
Value::String("deps".to_string()),
Value::Array(Box::new(Array {
values: mc.scope_analysis.get_deps(fn_.span),
})),
),
],
})),
@@ -60,8 +66,14 @@ impl Functionish {
),
(
Value::String("srcHash".to_string()),
src_hash_asm(source, arrow.span),
src_hash_asm(&mc.source, arrow.span),
),
// (
// Value::String("deps".to_string()),
// Value::Array(Box::new(Array {
// values: mc.scope_analysis.get_deps(arrow.span),
// })),
// ),
],
})),
Functionish::Constructor(_, _, _) => Value::Void,
@@ -239,7 +251,7 @@ impl<'a> FunctionCompiler<'a> {
};
let metadata_pointer = self.mc.allocate_defn_numbered("meta");
let metadata = functionish.metadata(&self.mc.source);
let metadata = functionish.metadata(self.mc);
self.fn_.metadata = Value::Pointer(metadata_pointer.clone());
self.set_owner_id(functionish.owner_id());

View File

@@ -71,7 +71,7 @@ pub struct ScopeAnalysis {
pub capture_values: HashMap<(OwnerId, NameId), Value>,
pub mutations: BTreeMap<swc_common::Span, NameId>,
pub optional_mutations: BTreeMap<swc_common::Span, NameId>,
pub refs: HashMap<swc_common::Span, Ref>,
pub refs: BTreeMap<swc_common::Span, Ref>,
pub diagnostics: RefCell<Vec<Diagnostic>>,
pub pointer_allocator: PointerAllocator,
pub reg_allocators: HashMap<OwnerId, RegAllocator>,
@@ -1994,6 +1994,34 @@ impl ScopeAnalysis {
res
}
pub fn get_deps(&self, span: swc_common::Span) -> Vec<Value> {
let start = swc_common::Span {
lo: span.lo,
hi: span.lo,
ctxt: span.ctxt,
};
let end = swc_common::Span {
lo: span.hi,
hi: span.hi,
ctxt: span.ctxt,
};
let mut deps = Vec::<Value>::new();
for (_span, ref_) in self.refs.range(start..end) {
let name = self.names.get(&ref_.name_id).unwrap();
if let Value::Register(_) = &name.value {
continue;
}
deps.push(name.value.clone());
}
deps
}
}
fn is_declare(decl: &swc_ecma_ast::Decl) -> bool {