Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add this.callers_promisers variable #2301

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions libpromises/eval_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,36 @@ void EvalContextClear(EvalContext *ctx)

}

Rlist *EvalContextGetPromiseCallerMethods(EvalContext *ctx) {
Rlist *callers = NULL;

for (size_t i = 0; i < SeqLength(ctx->stack); i++)
{
StackFrame *frame = SeqAt(ctx->stack, i);
switch (frame->type)
{
case STACK_FRAME_TYPE_BODY:
break;

case STACK_FRAME_TYPE_BUNDLE:
break;

case STACK_FRAME_TYPE_PROMISE_ITERATION:
break;

case STACK_FRAME_TYPE_PROMISE:
if (strcmp(frame->data.promise.owner->parent_promise_type->name, "methods") == 0) {
RlistAppendScalar(&callers, frame->data.promise.owner->promiser);
}
break;

case STACK_FRAME_TYPE_PROMISE_TYPE:
break;
}
}
return callers;
}

void EvalContextSetBundleArgs(EvalContext *ctx, const Rlist *args)
{
if (ctx->args)
Expand Down Expand Up @@ -1242,6 +1272,7 @@ void EvalContextStackPushPromiseFrame(EvalContext *ctx, const Promise *owner, bo
xsnprintf(v, sizeof(v), "%d", (int) ctx->ppid);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "promiser_ppid", v, CF_DATA_TYPE_INT, "source=agent");

EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "callers_promisers", EvalContextGetPromiseCallerMethods(ctx), CF_DATA_TYPE_STRING_LIST, "source=promise");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "bundle", PromiseGetBundle(owner)->name, CF_DATA_TYPE_STRING, "source=promise");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "namespace", PromiseGetNamespace(owner), CF_DATA_TYPE_STRING, "source=promise");
}
Expand Down
2 changes: 2 additions & 0 deletions libpromises/eval_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ ClassTableIterator *EvalContextClassTableIteratorNewLocal(const EvalContext *ctx

void EvalContextClear(EvalContext *ctx);

Rlist *EvalContextGetPromiseCallerMethods(EvalContext *ctx);

void EvalContextStackPushBundleFrame(EvalContext *ctx, const Bundle *owner, const Rlist *args, bool inherits_previous);
void EvalContextStackPushBodyFrame(EvalContext *ctx, const Promise *caller, const Body *body, const Rlist *args);
void EvalContextStackPushPromiseTypeFrame(EvalContext *ctx, const PromiseType *owner);
Expand Down
51 changes: 51 additions & 0 deletions tests/acceptance/21_methods/callers/01.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#######################################################
#
# Test the variable this.callers_promisers with one bundle
#
#######################################################

body common control
{
inputs => { "../../default.cf.sub" };
bundlesequence => { default("$(this.promise_filename)") };
version => "1.0";
}

#######################################################

bundle agent init {
}

bundle agent test {
methods:
"test" usebundle => "caller";
}

bundle agent check {
reports:
success::
"$(this.promise_filename) Pass";
!success::
"$(this.promise_filename) FAIL";
}

bundle agent caller {
methods:
"first call" usebundle => dummy;
}

bundle agent dummy {
vars:
"callers_promisers_expect" string => "any, any, test, first call";
"callers_promisers_actual" string => join(", ", "this.callers_promisers");

classes:
"success" expression => strcmp("${callers_promisers_expect}", "${callers_promisers_actual}"),
scope => "namespace";

reports:
DEBUG::
"EXPECT: callers_promisers_string = ${callers_promisers_expect}";
"ACTUAL: callers_promisers_string = ${callers_promisers_actual}";
}

70 changes: 70 additions & 0 deletions tests/acceptance/21_methods/callers/02.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#######################################################
#
# Test the variable this.callers_promisers with one bundle called twice
#
#######################################################

body common control
{
inputs => { "../../default.cf.sub" };
bundlesequence => { default("$(this.promise_filename)") };
version => "1.0";
}

#######################################################

bundle agent init {
}

bundle agent test {
methods:
"test" usebundle => "caller";
}

bundle agent check {
reports:
success_first.success_second::
"$(this.promise_filename) Pass";
!(success_first.success_second)::
"$(this.promise_filename) FAIL";
}

bundle agent caller {

methods:
"first call" usebundle => dummy;
"second call" usebundle => dummy_inter;

}

bundle agent dummy_inter {

methods:
"inter" usebundle => dummy;

}

bundle agent dummy {

vars:
# This bundle gets called twice, once directly, and once via dummy_inter

"callers_promisers_expect_first" string => "any, any, test, first call";
"callers_promisers_expect_second" string => "any, any, test, second call, inter";
"callers_promisers_actual" string => join(", ", "this.callers_promisers");

classes:
"success_first" expression => strcmp("${callers_promisers_expect_first}", "${callers_promisers_actual}"),
scope => "namespace";
"success_second" expression => strcmp("${callers_promisers_expect_second}", "${callers_promisers_actual}"),
scope => "namespace";

reports:
DEBUG::
"EXPECT (first in ${this.bundle}): callers_promisers_string = ${callers_promisers_expect_first}";
"ACTUAL (first in ${this.bundle}): callers_promisers_string = ${callers_promisers_actual}";

"EXPECT (second in ${this.bundle}): callers_promisers_string = ${callers_promisers_expect_second}";
"ACTUAL (second in ${this.bundle}): callers_promisers_string = ${callers_promisers_actual}";
}