Passing a variable by reference into a PHP7 extension - pass-by-reference

I followed the steps mentioned at Passing a variable by reference into a PHP extension for Passing a variable by reference into a PHP extension. This is working fine for PHP 5 but when I try the same in Php7 and its not working. Any suggestions? Here is my code snippet.
ZEND_BEGIN_ARG_INFO(params_ref_arg_arginfo, 0)
ZEND_ARG_INFO(1, a)
ZEND_END_ARG_INFO()
PHP_FUNCTION(sample_byref_compiletime)
{
zval *a;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a ) == FAILURE)
{
php_printf("Error");
RETURN_NULL();
}
zval_dtor(a);
ZVAL_LONG(a, 40);
}
PHP_FE(sample_byref_compiletime, params_ref_arg_arginfo)
Thank you for the help.

Change from "z" to "z/". View details in https://wiki.php.net/phpng-upgrading. Possible type specifiers http://php.net/manual/en/internals2.funcs.php.
Also you can change your code to:
PHP_FUNCTION(sample_byref_compiletime)
{
zval *a;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z", &a ) == FAILURE)
{
php_printf("Error");
RETURN_NULL();
}
ZVAL_DEREF(a);
SEPARATE_ZVAL_NOREF(a);
zval_dtor(a);
ZVAL_LONG(a, 40);
}

Related

How to use TCppWebBrowser on RAD 10.4

The code below used to be perfect to C++Builder 6.0, but it is not compiling on RAD 10.4 Sydney.
I am not familiar with OLE controls and its classes and methods.
Can someone help me to make it work?
PogBrowser is a TCppWebBrowser.
void __fastcall TRelatPOG::ShowStream( TStream *stm )
{
try
{
if( !PogBrowser->Document )
{
PogBrowser->Navigate(L"about:blank");
while( PogBrowser->ReadyState != 4 )
Application->ProcessMessages();
}
IPersistStreamInit *psi;
TStreamAdapter *sa = new TStreamAdapter(stm,soReference);
if( sa )
{
if (SUCCEEDED(PogBrowser->Document->QueryInterface(IID_IPersistStreamInit,(void **)&psi)))
{
psi->InitNew();
psi->Load(*sa);// Compile error
psi->Release();
}
delete sa;
}
}
catch(Exception *E)
{
MessageDlg(String(E->ClassName()) + " ( " + E->Message + " )", mtError, TMsgDlgButtons() << mbOK, 0);
}
}
Once upon a time, TStreamAdapter used to implicitly convert to IStream*, but now it implicitly converts to _di_IStream instead (ie DelphiInterface<IStream>).
IPersistStreamInit::Save() requires IStream*, thus requires 2 conversions (TStreamAdapter -> _di_IStream -> IStream*), but C++ only allows 1 implicit conversion at a time.
So, you need to cast the TStreamAdapter to _di_IStream explicitly, which can then convert to IStream* implicitly, eg:
psi->Load(static_cast<_di_IStream>(*sa));
However, a better solution would be to let _di_IStream handle the lifetime of the TStreamAdapter to begin with, eg:
_di_IPersistStreamInit psi;
if (SUCCEEDED(PogBrowser->Document->QueryInterface(IID_IPersistStreamInit, reinterpret_cast<void**>(&psi))))
{
_di_IStream sa(*(new TStreamAdapter(stm, soReference)));
psi->InitNew();
psi->Load(sa);
}

Specman macros: How to use optional tags?

I have a macro for defining ports:
-- Create simple port
define <p_def'struct_member> "p_def <name'name> <type'type>" as {
<name'name> : inout simple_port of <type'type> is instance;
keep bind(<name'name>,empty);
};
I would like to extend the macro to support a list of ports. I would like to use for it an optional tag [<len'name>] that defines the list size, something like this:
-- Create simple port OR list of simple ports
define <p_def'struct_member> "p_def <name'name> [\[<len'name>\] ] <type'type>" as {
if len does not exist { // How to implement it?
<name'name> : inout simple_port of <type'type> is instance;
keep bind(<name'name>,empty);
} else { // len exists
<name'name>[<len'name>] : list of inout simple_port of <type'type> is instance;
keep for each in <name'name> {
bind(it,empty);
};
};
};
** For example, defining a list of ports of size 10 will look this way:
p_def my_list_of_ports[10] bit;
I cannot find in Specman e Language Reference how can I know if the optional tag ([<len'name>]) is defined or not.. Do you know how to implement the "if len does not exist" statement in macro?
Thank you for your help
In case someone is interested what is the answer:
define <p_def'struct_member> "p_def <name'name>[\[<len'name>\]] <type'type>" as computed {
if <len'name> == NULL {
result = appendf("%s : inout simple_port of %s is instance; \n keep bind(%s,empty);", <name'name>, <type'type>, <name'name>);
} else { // list
result = appendf("%s[%s] : list of inout simple_port of %s is instance; keep for each in %s { bind(it,empty); };", <name'name>, <len'name>, <type'type>, <name'name>);
};
};

SweetJS : Write a macro for a specific library

I'm currently working on a little project which consists about writing macros for Ramda. Here is an example :
let map = macro {
rule { $f $array } => { R.map($f, $array) }
rule { $f } => { R.map($f) }
}
I tried to compile this simple sample of code as a beginning :
var R = require('ramda');
function log (value) {
console.log(value);
}
map log [1, 2, 3];
Because of hygiene, the compiled code looks like this :
var R$759 = require('ramda');
function log$761(value$762) {
console.log(value$762);
}
R.map(log$761)[1, 2, 3];
My problem is that I don't know how to make reference to ramda.
Has anyone tried to write macros for a specific library and encountered this problem ?
At the moment the ways to do it are a little hacky. In the next release when we get ES6 modules this will actually be taken care of automatically for you but until then the best option is to have an initialization macro (this is what ki and contracts.js do). This works by having a shared variable in scope for all of your macros and then having the user first invoke an import macro that does the necessary require:
var r_lib;
macro import {
rule { $name from $path } => {
r_lib = require($path);
}
}
let map = macro {
rule { $f $l } => { r_lib.map($f, $l) }
}
import R from "ramda"
map log [1,2,3]

is there any way to use .indexOf to search a javascript array in mirth?

I am trying to find a string in a javascript array in the transformer of a mirth channel. Mirth throws an error when I try to use indexOf function. My understanding is that indexOf is something that browsers add in, rather than a native part of the javascript language itself. ( How do I check if an array includes an object in JavaScript? )
So is array.indexOf just not supported in Mirth? Is there any way to use .indexOf in Mirth? Maybe an alternate syntax? Or do I need to just loop thru the array to search?
This is how I search arrays in a Mirth js transformer:
var Yak = [];
Yak.push('test');
if(Yak.indexOf('test') != -1)
{
// do something
}
Does this give you error?
Mirth uses the Rhino engine for Javascript, and on some earlier versions of the JVM, indexOf appeared to not be supported on arrays. Since upgrading our JVM to 1.6.23 (or higher), indexOf has started working. However, we still have legacy code that, when searching arrays of strings, I just use a loop each time:
var compareString = "blah";
var index = -1;
for (var i = 0; i < myArray.length; ++i)
{
if (myArray[i] == compareString)
{
index = i;
break;
}
}
If you need to do this frequently, you should be able to use a code template to manually add the indexOf function to Array.
Set the code template to global access, and try out something like this (untested code):
Array.prototype.indexOf = function(var compareObject)
{
for (var i = 0; i < myArray.length; ++i)
{
// I don't think this is actually the right way to compare
if (myArray[i] == compareObject)
{
return i;
}
}
return -1;
}
var arr = ['john',1,'Peter'];
if(arr.indexOf('john') > -1)
{
//match. what to do?
console.log("found");
}
else
{
console.log("not found");//not found .. do something
}
var i = ['a', 'b', 'c']
if(i.indexOf('a') > -1)
{
///do this, if it finds something in the array that matches what inside the indexOf()
}
else
{
//do something else if it theres no match in array
}

specman: Assign multiple struct member in one expression

Hy,
I expanding an existing specman test where some code like this appears:
struct dataset {
!register : int (bits:16);
... other members
}
...
data : list of dataset;
foo : dataset;
gen foo;
foo.register = 0xfe;
... assign other foo members ...
data.push(foo.copy());
is there a way to assign to the members of the struct in one line? like:
foo = { 0xff, ... };
I currently can't think of a direct way of setting all members as you want, but there is a way to initialize variables (I'm not sure if it works on struct members as well). Anyway something like the following may fit for you:
myfunc() is {
var foo : dataset = new dataset with {
.register = 0xff;
.bar = 0xfa;
}
data.push(foo.copy());
}
You can find more information about new with help new struct from the specman prompt.
Hope it helps!
the simple beuty of assigning fields by name is one language feature i've always found usefull , safe to code and readable.
this is how i'd go about it:
struct s {
a : int;
b : string;
c : bit;
};
extend sys {
ex() is {
var s := new s with {.a = 0x0; .b = "zero"; .c = 0;};
};
run() is also {
var s;
gen s keeping {.a == 0x0; .b == "zero"; .c == 0;};
};
};
i even do data.push(new dataset with {.reg = 0xff; bar = 0x0;}); but you may raise the readablity flag if you want.
warning: using unpack() is perfectly correct (see ross's answer), however error prone IMO. i recommend to verify (with code that actually runs) every place you opt to use unpack().
You can directly use the pack and unpack facility of Specman with "physical fields" ( those instance members prefixed with the modifier %).
Example:
define FLOODLES_WIDTH 47;
type floodles_t : uint(bits:FLOODLES_WIDTH);
define FLABNICKERS_WIDTH 28;
type flabnickers_t : uint(bits:FLABNICKERS_WIDTH);
struct foo_s {
%!floodle : floodles_t;
%!flabnicker : flabnickers_t;
};
extend sys {
run() is also {
var f : foo_s = new;
unpack(packing.low,64'hdeadbeefdeadbeef,f);
print f;
unpack(packing.low,64'hacedacedacedaced,f);
print f;
};
setup() is also {
set_config(print,radix,hex);
};
};
When this run, it prints:
Loading /nfs/pdx/home/rbroger1/tmp.e ...
read...parse...update...patch...h code...code...clean...
Doing setup ...
Generating the test using seed 1...
Starting the test ...
Running the test ...
f = foo_s-#0: foo_s of unit: sys
---------------------------------------------- #tmp
0 !%floodle: 0x3eefdeadbeef
1 !%flabnicker: 0x001bd5b
f = foo_s-#0: foo_s of unit: sys
---------------------------------------------- #tmp
0 !%floodle: 0x2cedacedaced
1 !%flabnicker: 0x00159db
Look up packing, unpacking, physical fields, packing.low, packing.high in your Specman docs.
You can still use physical fields even if the struct doesn't map to the DUT. If your struct is already using physical fields for some other purpose then you'll need to pursue some sort of set* method for that struct.