/* Copyright (c) 2020 Contrast Security, Inc. See * https://www.contrastsecurity.com/enduser-terms-0317a for more details. */ #include "cs__scope.h" #include VALUE in_given_scope(const VALUE object, const char *scope) { VALUE level; level = rb_iv_get(object, scope); if (NUM2INT(level) > 0) { return Qtrue; } return Qfalse; } void enter_given_scope(const VALUE object, const char *scope) { int level = NUM2INT(rb_iv_get(object, scope)); rb_iv_set(object, scope, INT2NUM(level + 1)); } void exit_given_scope(const VALUE object, const char *scope) { int level = NUM2INT(rb_iv_get(object, scope)); rb_iv_set(object, scope, INT2NUM(level - 1)); } VALUE in_contrast_scope(const VALUE self) { return in_given_scope(self, ivar_contrast_scope); } VALUE enter_contrast_scope(const VALUE self) { enter_given_scope(self, ivar_contrast_scope); return Qnil; } VALUE exit_contrast_scope(const VALUE self) { exit_given_scope(self, ivar_contrast_scope); return Qnil; } VALUE run_in_scope(const VALUE self) { enter_contrast_scope(self); rb_ensure(rb_yield, Qundef, exit_contrast_scope, self); return Qnil; } VALUE enter_scope_for(const VALUE self, const VALUE scope_symbol) { enter_contrast_scope(self); return Qnil; } VALUE exit_scope_for(const VALUE self, const VALUE scope_symbol) { exit_contrast_scope(self); return Qnil; } VALUE initialize(const VALUE self) { rb_iv_set(self, ivar_contrast_scope, rbzero); return self; } VALUE deep_clone(const VALUE self) { VALUE new_scope = rb_funcall(scope_class, rb_sym_new, 0); rb_iv_set(new_scope, ivar_contrast_scope, rb_iv_get(self, ivar_contrast_scope)); return new_scope; } void Init_cs__scope(void) { rb_sym_new = rb_intern("new"); VALUE contrast = rb_define_module("Contrast"); VALUE agent = rb_define_module_under(contrast, "Agent"); scope_class = rb_define_class_under(agent, "Scope", rb_cObject); rb_define_method(scope_class, "initialize", initialize, 0); ivar_contrast_scope = "@contrast_scope"; rb_define_const(scope_class, "CONTRAST_SCOPE", ID2SYM(rb_intern("contrast"))); CONTRAST_SCOPE = rb_const_get(scope_class, rb_intern("CONTRAST_SCOPE")); rb_define_method(scope_class, "in_contrast_scope?", in_contrast_scope, 0); rb_define_method(scope_class, "enter_contrast_scope", enter_contrast_scope, 0); rb_define_method(scope_class, "exit_contrast_scope", exit_contrast_scope, 0); rb_define_method(scope_class, "run_in_scope", run_in_scope, 0); rb_define_method(scope_class, "enter_scope_for", enter_scope_for, 1); rb_define_method(scope_class, "exit_scope_for", exit_scope_for, 1); rb_define_method(scope_class, "deep_clone", deep_clone, 0); }