Assign null or reset variable


#1

I’m interested in running something like the following:

if(!this.target.is_var_null()){this.target.reset();}

or

if(!this.target.is_var_null()){this.target = nullptr;}

The issue I’m having is this.target is not a shared_ptr of the type, but is being treated as a reference or something, so I cannot call .reset() on it even after registering the following:

a_script.add(chaiscript::fun([](std::shared_ptr<Creature> &a_self) {
	a_self.reset();
}), "reset");

Is there a way in chaiscript to make is_var_null true again once you’ve assigned to the variable?


#2

There is not, currently, any way to reset a variable. Although, I would generally expect the version you show to work.

There’s a couple of questions at play:

  1. Do you want to set a typed value to null?
  2. Or do you want to reset the ChaiScript variable all the way back to being undefined?

#3

I want to assign null, but for my use case it would be functionally equivalent either way. Number 2 would allow you to essentially “change” the type of a variable which I don’t think is necessary and that’s why I say “assign null”.

I tried reference assigning nullptr (after binding it in c++ with add_global) but got a type mismatch exception.

So far I’m stumped.


#4

Some small examples:

C++:

	a_script.add(chaiscript::fun([]() {
		return std::make_shared<int>(1);
	}), "GetSharedInt");

	a_script.add(chaiscript::fun([](std::shared_ptr<int> &a_self) {
		a_self.reset();
	}), "reset");

chaiscript:

auto testVariable;
if(testVariable.is_var_null()){
	testVariable = GetSharedInt();
	print("Assigned");
	print(testVariable);
}

testVariable.reset();

if(testVariable.is_var_null()){
	print("This is expected");
}

Output:

Assigned
1
Error: “Error with function dispatch for function ‘reset’” With parameters: (int).()


#5

I just went on a run to think this over - I think the issue is with a non-const & to shared_ptr<> in my boxed_cast mechanism.

I’ll look into it as soon as I can. Possibly later today.

-Jason


#6

I can already confirm that that is in fact the issue.

Are you operating on the 5.8.1 release? Or off of develop? Or something else?


#7

I’m on 5.8.1 downloaded the zip off the site.

I think getting .reset working is important as a general bug fix. But also think assignment to null would make a lot of sense as a general language feature. Since chaiscript has built in null checks via “is_var_null” I would also intuitively expect to be able to do:

MyVariable = null;

Or

if(MyVariable == null)

Anyway, pretty impressed with this language so far, and thank you for your responsiveness!!


#8

The problem I see right now is that I make assumptions that the pointed-to memory location never changes. And I cache the pointers and use that to optimize some aspects.

I’m not sure if I can change that assumption without having a bad impact on performance.

I’ll need to handle this in a way that doesn’t affect the normal case, but still allows you to reset a pointer, and possibly reset the entire object.


#9

“There are only two hard things in Computer Science: cache invalidation and naming things.”

– Phil Karlton


#10

Check out this commit: https://github.com/ChaiScript/ChaiScript/commit/91a3ae1f14c9aca383d6f8f257d46d4007c29902

Let me know if that version works for you. You should be able to download it from here: https://github.com/ChaiScript/ChaiScript/archive/release-5.x.zip

-Jason


#11

Still having a problem with .reset()

I downloaded that release-5.x.zip and tried it out.

“Error with function dispatch for function ‘reset’” with parameters: (int).()


As an aside: I’d love to be able to use null assignment instead of .reset, but that’s more of a “nice to have” as this other method would be a viable work-around. The null assignment is more generic and more useful for types in the future (if I’m downloading a script I don’t want to have to have the foresight to expose a null assign to every single type of shared_ptr, and I’m not sure about bare pointers)


#12

Hmm, that’s quite strange.

testVariable.reset(); // fails to dispatch
reset(testVariable); // works

#13

OK, issue found and fixed. Give it another try with the latest from that branch.

-Jason


#14

I’ll give it a go later today or possibly tomorrow and report back!


#15

Seems to work now!

Also I used to have some issues with min/max defines before which required me to change something in chaiscript (I just manually undef’d min and max and got rid of the pragma pop macro stuff) But I didn’t need to this time, I’m guessing you fixed that too (or my compiler is just behaving nicer now).

Thanks!