V8: How to use Set method on a Persistent handler? - v8

I need to set properties on an object referenced by a Persistent handler. However, it seems like Persistent class doesn't have a Set method (unlike the Local class).
Does it mean that Persistent handlers are read only? What is the best way to set a property on a Persistent handler then?

Create a v8::Local from the v8::Persistent, then use that to call operations on the object (Set and/or others). Note that you need an active HandleScope for this.
v8::Persistent<v8::Object> my_persistent = ...;
v8::HandleScope scope(isolate);
v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(isolate, my_persistent);
obj->Set(...);
(To be nitpicky: Local doesn't have a Set method either; what it does have is an operator-> overload that lets you call methods (including Set) on the thing that the Local refers to. If that thing isn't a v8::Object but e.g. a v8::Number, then it won't have a Set method.)

Related

Wrapping instance variables in accessor methods

Sandy Metz says (POODR book, page 26):
Because it is possible to wrap every instance variable in a method and to therefore treat any variable as if it's just another object, the distinction between data and a regular object begins to disappear.
I am not sure if I understand what she is explaining. When we define the accessors, we are wrapping the instance variables (data) on a method but methods are not objects. So what does she mean when she says that we can treat variables as if they're just another object?
The primary difference between data and objects is behaviour. Objects can modify their internal state without changing their interfaces, while data are static structures.
When we wrap data access within a method, we get the same benefits of an object - the interface remains static to consumers even if the underlying data structure needs to change.

Magento: How to pass a variable from block to a Model

I am using an extension where I need to pass the variable is moving from block class back to model, I have used session it works sometime but not uniformly,
Is there any other way to pass it,
Thanks in advance,
try using Magento’s Registry Pattern
The three registry methods are
Mage::register
Mage::unregister
Mage::registry
The register method is how you set a global-like variable.
Mage::register('some_name', $var);
Then, later in the request execution, (from any method), you can fetch your variable back out
$my_var = Mage::registry('some_name');
Finally, if you want to make you variable unavailable, you can use the unregister method to remove it from the registry.
Mage::unregister('some_name');
Source
If you can use the Model as a singleton, you can try this:
Mage::getSingleton('yourmodule/yourmodel')->setStuff('xxxx');
and later
Mage::getSingleton('yourmodule/yourmodel')->getStuff();
if you don't know what singletons are you should maybe try the registry approach from epynic to prevent problems.

How do automatically generated instance variables work in NSManagedObjects?

Xcode 4.5 and later auto-synthesizes properties, making an instance variable with the underscore prepended on the property name. But how does this work in an NSManagedObject? They want you to use KVC primitive methods in your custom setters. So what happens if you set an instance variable via the underscore ivar inside the NSManagedObject? Won't that screw things up since it would bypass the KVC methods? Or is it safely doing this behind the scenes?
If you access the underscore instance variable directly, you are bypassing the work that NSManagedObject does for you. You should use the get and set accessor methods that NSManagedObject auto-generates for your attributes.
Apple's documentation states
When you access or modify properties of a managed object, you should
use these [accessor] methods directly.
You can implement your own accessor methods if required, but in that case, you have to do additional work beyond changing the value of the instance variable:
You must ensure that you invoke the relevant access and change
notification methods (willAccessValueForKey:, didAccessValueForKey:,
willChangeValueForKey:, didChangeValueForKey:,
willChangeValueForKey:withSetMutation:usingObjects:, and
didChangeValueForKey:withSetMutation:usingObjects:).
This should illustrate that you can't get the correct behavior simply by modifying the instance variable directly.
Note that unlike ordinary properties, NSManagedObject properties are not synthesized at compile time (hence the use of #dynamic for the implementation). Since compile-time synthesis isn't used, there are no synthesized instance variables available for you to set.
Instead, instances of NSManagedObject have a private internal instance of something similar to an NSMutableDictionary to store their state. The dynamically generated property accessors are wrappers for calls to KVC-like methods that access the private storage.

How to correctly use Context::Scope ?

How to correctly use Context::Scope ? Do i allocate it while within a method/function scope while actually executing something or can i have a global copy of it next to the Context object ? The documentation isn't very clear on that.
I want to make a global Application class and keep all the persistent stuff there not sure if i can put context scope there.
Context::Scope has to be allocated on the call stack outside of your call chain. What you want to do is use a Persistent<Context> to store your Context object globally, and then create a Context::Scope when you enter a JavaScript call stack. This is the case for all of V8's scope objects (HandleScope, Isolate::Scope, etc.).

Can a delegate instance be defined outside of a class?

From what I am reading, a delegate instance is always defined with a class as an input or inside a class.
Why can't I define a delegate instance independently?
A delegate is a type, and you can define it at namespace scope (including the global namespace).
Since delegates are reference types, delegate instances always are placed on the managed (garbage collected) heap. Delegate instances can be created with the gcnew operator, the Delegate::CreateDelegate method, or using stack semantics syntax (C++/CLI only).
A reference variable of delegate type (including stack semantics syntax variables which wrap a permanently-bound reference, an instance on the heap, and an automatic call to IDisposable::Dispose) can exist as an instance or static member of a managed type, an automatic local variable, a static local variable, or (in C++/CLI) as a global (namespace scope) variable.

Resources