git.delta.rocks / jrsonnet / refs/commits / 643b0073bda6

difftreelog

feat support standalone super

Yaroslav Bolyukin2021-02-20parent: #7b5cb9f.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -24,8 +24,8 @@
 	NoTopLevelObjectFound,
 	#[error("self is only usable inside objects")]
 	CantUseSelfOutsideOfObject,
-	#[error("super is only usable inside objects")]
-	CantUseSuperOutsideOfObject,
+	#[error("no super found")]
+	NoSuperFound,
 
 	#[error("for loop can only iterate over arrays")]
 	InComprehensionCanOnlyIterateOverArray,
modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
418 Literal(LiteralType::This) => {418 Literal(LiteralType::This) => {
419 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)419 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
420 }420 }
421 Literal(LiteralType::Super) => Val::Obj(
422 context
423 .super_obj()
424 .clone()
425 .ok_or(NoSuperFound)?
426 .with_this(context.this().clone().unwrap()),
427 ),
421 Literal(LiteralType::Dollar) => {428 Literal(LiteralType::Dollar) => {
422 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)429 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)
423 }430 }
434 || format!("variable <{}>", name),441 || format!("variable <{}>", name),
435 || Ok(context.binding(name.clone())?.evaluate()?),442 || Ok(context.binding(name.clone())?.evaluate()?),
436 )?,443 )?,
437 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
438 let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;
439 context
440 .super_obj()
441 .clone()
442 .expect("no super found")
443 .get_raw(name, Some(&context.this().clone().expect("no this found")))?
444 .expect("value not found")
445 }
446 Index(value, index) => {444 Index(value, index) => {
447 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {445 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {
448 (Val::Obj(v), Val::Str(s)) => {446 (Val::Obj(v), Val::Str(s)) => {
620 import_location.pop();618 import_location.pop();
621 Val::Str(with_state(|s| s.import_file_str(import_location, path))?)619 Val::Str(with_state(|s| s.import_file_str(import_location, path))?)
622 }620 }
623 Literal(LiteralType::Super) => throw!(StandaloneSuper),
624 })621 })
625}622}
626623
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -931,6 +931,25 @@
 		Ok(())
 	}
 
+	#[test]
+	fn standalone_super() -> crate::error::Result<()> {
+		assert_eval!(
+			r#"
+			local obj = {
+				a: 1,
+				b: 2,
+				c: 3,
+			};
+			local test = obj + {
+				fields: std.objectFields(super),
+				d: 5,
+			};
+			test.fields == ['a', 'b', 'c']
+		"#
+		);
+		Ok(())
+	}
+
 	struct TestImportResolver(IStr);
 	impl crate::import::ImportResolver for TestImportResolver {
 		fn resolve_file(&self, _: &PathBuf, _: &PathBuf) -> crate::error::Result<Rc<PathBuf>> {