mirror of
https://github.com/zeek/zeek.git
synced 2025-10-13 12:08:20 +00:00
Merge remote-tracking branch 'origin/topic/jsiwek/type-alias-introspection'
* origin/topic/jsiwek/type-alias-introspection: Add enum_names() BIF to return names of an enum type's values Add type_aliases() BIF for introspecting type-names of types/values Change Type::type_aliases map to store IntrusivePtr Fix lookup_ID() BIF to return enum values
This commit is contained in:
commit
aab99b743d
12 changed files with 301 additions and 13 deletions
13
testing/btest/Baseline/bifs.enum_names/out
Normal file
13
testing/btest/Baseline/bifs.enum_names/out
Normal file
|
@ -0,0 +1,13 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||
{
|
||||
RED,
|
||||
GREEN,
|
||||
PURPLE,
|
||||
BLUE
|
||||
}
|
||||
{
|
||||
RED,
|
||||
GREEN,
|
||||
PURPLE,
|
||||
BLUE
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||
zeek test
|
||||
<unknown id>
|
||||
<unknown id>
|
||||
<unknown id>
|
||||
GREEN
|
||||
event()
|
||||
|
|
22
testing/btest/Baseline/bifs.type_aliases/out
Normal file
22
testing/btest/Baseline/bifs.type_aliases/out
Normal file
|
@ -0,0 +1,22 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||
type aliases for 'RED enum val': ColorAlias Color
|
||||
type aliases for 'Color enum type': ColorAlias Color
|
||||
type aliases for 'MyRec val': MyRec MyRecAlias
|
||||
type aliases for 'MyRecAlias val': MyRec MyRecAlias
|
||||
type aliases for 'MyRec type': MyRec MyRecAlias
|
||||
type aliases for 'MyRecalias type': MyRec MyRecAlias
|
||||
type aliases for 'MyString val': it's just a 'string'
|
||||
type aliases for 'MyString type': MyString AnotherString
|
||||
type aliases for 'MyOtherString type': MyOtherString
|
||||
type aliases for 'AnotherString type': MyString AnotherString
|
||||
type aliases for 'string literal value': it's just a 'string'
|
||||
type aliases for 'count literal value': it's just a 'count'
|
||||
type aliases for 'MyTable value': MyTable2 MyTable3 MyTable MyTable4
|
||||
type aliases for 'MyTable2 value': MyTable2 MyTable3 MyTable MyTable4
|
||||
type aliases for 'MyTable3 value': MyTable2 MyTable3 MyTable MyTable4
|
||||
type aliases for 'MyTable4 value': MyTable2 MyTable3 MyTable MyTable4
|
||||
type aliases for 'MyTable type': MyTable2 MyTable3 MyTable MyTable4
|
||||
type aliases for 'MyTable2 type': MyTable2 MyTable3 MyTable MyTable4
|
||||
type aliases for 'MyTable3 type': MyTable2 MyTable3 MyTable MyTable4
|
||||
type aliases for 'MyTable4 type': MyTable2 MyTable3 MyTable MyTable4
|
||||
type aliases for 'table value': it's just a 'table[count] of string'
|
10
testing/btest/bifs/enum_names.zeek
Normal file
10
testing/btest/bifs/enum_names.zeek
Normal file
|
@ -0,0 +1,10 @@
|
|||
# @TEST-EXEC: zeek -b %INPUT >out
|
||||
# @TEST-EXEC: btest-diff out
|
||||
|
||||
type Color: enum { RED, GREEN, BLUE };
|
||||
type ColorAlias: Color;
|
||||
|
||||
redef enum Color += { PURPLE };
|
||||
|
||||
print enum_names(Color);
|
||||
print enum_names(ColorAlias);
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
global a = "zeek test";
|
||||
|
||||
type Color: enum { RED, GREEN, BLUE };
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
local b = "local value";
|
||||
|
@ -12,5 +14,6 @@ event zeek_init()
|
|||
print lookup_ID("");
|
||||
print lookup_ID("xyz");
|
||||
print lookup_ID("b");
|
||||
print lookup_ID("GREEN");
|
||||
print type_name( lookup_ID("zeek_init") );
|
||||
}
|
||||
|
|
60
testing/btest/bifs/type_aliases.zeek
Normal file
60
testing/btest/bifs/type_aliases.zeek
Normal file
|
@ -0,0 +1,60 @@
|
|||
# @TEST-EXEC: zeek -b %INPUT >out
|
||||
# @TEST-EXEC: btest-diff out
|
||||
|
||||
type MyRec: record {
|
||||
a: bool &optional;
|
||||
};
|
||||
|
||||
type Color: enum { RED, GREEN, BLUE };
|
||||
type ColorAlias: Color;
|
||||
|
||||
type MyRecAlias: MyRec;
|
||||
type MyString: string;
|
||||
type MyOtherString: string;
|
||||
type AnotherString: MyString;
|
||||
type MyTable: table[count] of string;
|
||||
type MyTable2: MyTable;
|
||||
type MyTable3: MyTable2;
|
||||
type MyTable4: MyTable3;
|
||||
|
||||
function type_alias_list(label: string, x: any): string
|
||||
{
|
||||
local rval = fmt("type aliases for '%s':", label);
|
||||
local aliases = type_aliases(x);
|
||||
|
||||
if ( |aliases| == 0 )
|
||||
rval += fmt(" it's just a '%s'", type_name(x));
|
||||
else
|
||||
for ( a in aliases )
|
||||
rval += fmt(" %s", a);
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
print type_alias_list("RED enum val", RED);
|
||||
print type_alias_list("Color enum type", Color);
|
||||
|
||||
print type_alias_list("MyRec val", MyRec());
|
||||
print type_alias_list("MyRecAlias val", MyRecAlias());
|
||||
|
||||
print type_alias_list("MyRec type", MyRec);
|
||||
print type_alias_list("MyRecalias type", MyRecAlias);
|
||||
|
||||
local mys: MyString = "hi";
|
||||
print type_alias_list("MyString val", mys);
|
||||
print type_alias_list("MyString type", MyString);
|
||||
print type_alias_list("MyOtherString type", MyOtherString);
|
||||
print type_alias_list("AnotherString type", AnotherString);
|
||||
|
||||
print type_alias_list("string literal value", "test");
|
||||
print type_alias_list("count literal value", 7);
|
||||
|
||||
print type_alias_list("MyTable value", MyTable([1] = "one", [2] = "two"));
|
||||
print type_alias_list("MyTable2 value", MyTable2([1] = "one", [2] = "two"));
|
||||
print type_alias_list("MyTable3 value", MyTable3([1] = "one", [2] = "two"));
|
||||
print type_alias_list("MyTable4 value", MyTable4([1] = "one", [2] = "two"));
|
||||
print type_alias_list("MyTable type", MyTable);
|
||||
print type_alias_list("MyTable2 type", MyTable2);
|
||||
print type_alias_list("MyTable3 type", MyTable3);
|
||||
print type_alias_list("MyTable4 type", MyTable4);
|
||||
print type_alias_list("table value", table([1] = "one", [2] = "two"));
|
Loading…
Add table
Add a link
Reference in a new issue