Skip to content

Commit bc72604

Browse files
oulgenfacebook-github-bot
authored andcommitted
Add a function to convert string into lazyclass
Summary: In order to unblock modularity migration, we need a backdoor to allow converting strings to lazy classes. Here's a simple implementation. Reviewed By: jamesjwu Differential Revision: D40284561 fbshipit-source-id: 0077e6f161b344aab0ac67b7e69d2640c759b41c
1 parent 81b528e commit bc72604

File tree

6 files changed

+31
-0
lines changed

6 files changed

+31
-0
lines changed

hphp/runtime/ext/hh/ext_hh.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,10 @@ TypedValue HHVM_FUNCTION(dynamic_class_meth_force, StringArg cls,
10111011
return dynamicClassMeth<DynamicAttr::Ignore, false>(cls.get(), meth.get());
10121012
}
10131013

1014+
TypedValue HHVM_FUNCTION(classname_from_string_unsafe, StringArg cls) {
1015+
return make_tv<KindOfLazyClass>(LazyClassData::create(cls.get()));
1016+
}
1017+
10141018
namespace {
10151019

10161020
const StaticString
@@ -1480,6 +1484,7 @@ static struct HHExtension final : Extension {
14801484
X(dynamic_fun_force);
14811485
X(dynamic_class_meth);
14821486
X(dynamic_class_meth_force);
1487+
X(classname_from_string_unsafe);
14831488
X(enable_per_file_coverage);
14841489
X(disable_per_file_coverage);
14851490
X(get_files_with_coverage);

hphp/runtime/ext/hh/ext_hh.php

+7
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ function dynamic_fun_force(string $name)[]: mixed;
255255
<<__Native>>
256256
function dynamic_class_meth_force(string $cls, string $meth)[]: mixed;
257257

258+
/**
259+
* Creates a LazyClass pointer from input $classname. It does not eagerly
260+
* verify that the $classname is in fact a valid class.
261+
*/
262+
<<__Native>>
263+
function classname_from_string_unsafe(string $classname)[]: mixed;
264+
258265
/**
259266
* Begin collecting code coverage on all subsequent calls into files in $files
260267
* during this request.

hphp/runtime/ext/std/ext_std_intrinsics.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ Variant HHVM_FUNCTION(create_clsmeth_pointer, StringArg cls, StringArg meth) {
399399
return Variant{ClsMethDataRef::create(c, m)};
400400
}
401401

402+
bool HHVM_FUNCTION(is_lazy_class, TypedValue val) {
403+
return tvIsLazyClass(val);
404+
}
405+
402406
///////////////////////////////////////////////////////////////////////////////
403407

404408
bool HHVM_FUNCTION(is_unit_loaded, StringArg path) {
@@ -522,6 +526,7 @@ void StandardExtension::initIntrinsics() {
522526
HHVM_FALIAS(__hhvm_intrinsics\\create_class_pointer, create_class_pointer);
523527
HHVM_FALIAS(__hhvm_intrinsics\\create_clsmeth_pointer,
524528
create_clsmeth_pointer);
529+
HHVM_FALIAS(__hhvm_intrinsics\\is_lazy_class, is_lazy_class);
525530

526531
HHVM_FALIAS(__hhvm_intrinsics\\is_unit_loaded, is_unit_loaded);
527532
HHVM_FALIAS(__hhvm_intrinsics\\drain_unit_prefetcher, drain_unit_prefetcher);

hphp/runtime/ext/std/ext_std_intrinsics.php

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ function create_class_pointer(string $name): mixed;
7676
<<__Native>>
7777
function create_clsmeth_pointer(string $cls, string $meth): mixed;
7878

79+
<<__Native>>
80+
function is_lazy_class(mixed $val): bool;
81+
7982
<<__Native>>
8083
function dummy_lots_inout(inout $p1, inout $p2, inout $p3, inout $p4,
8184
inout $p1, inout $p2, inout $p3, inout $p4,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?hh
2+
3+
class C {}
4+
5+
<<__EntryPoint>>
6+
function main() {
7+
var_dump(__hhvm_intrinsics\is_lazy_class(HH\classname_from_string_unsafe("C")));
8+
var_dump(__hhvm_intrinsics\is_lazy_class(HH\classname_from_string_unsafe("WHOAMI")));
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bool(true)
2+
bool(true)

0 commit comments

Comments
 (0)