Skip to content

Commit

Permalink
cl_abap_weak_reference + osql workaround (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshp authored Aug 12, 2023
1 parent 7603890 commit 409a039
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"description": "test",
"scripts": {
"lint": "abaplint",
"unit": "rm -rf output && abap_transpile && echo RUNNING && node output/index.mjs && echo OK",
"integration": "rm -rf output_test && abap_transpile ./abap_transpile_test.json && node output_test/index.mjs",
"unit": "rm -rf output && abap_transpile && echo RUNNING && node --expose-gc output/index.mjs && echo OK",
"integration": "rm -rf output_test && abap_transpile ./abap_transpile_test.json && node --expose-gc output_test/index.mjs",
"test": "npm run lint && npm run unit"
},
"license": "MIT",
Expand Down
24 changes: 24 additions & 0 deletions src/abap/cl_abap_weak_reference.clas.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CLASS cl_abap_weak_reference DEFINITION PUBLIC.
PUBLIC SECTION.
METHODS constructor
IMPORTING
oref TYPE REF TO object.

METHODS get
RETURNING
VALUE(oref) TYPE REF TO object.
PRIVATE SECTION.
DATA mv_ref TYPE x LENGTH 1.
ENDCLASS.

CLASS cl_abap_weak_reference IMPLEMENTATION.
METHOD constructor.
ASSERT oref IS NOT INITIAL.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef
WRITE '@KERNEL this.mv_ref = new WeakRef(oref);'.
ENDMETHOD.

METHOD get.
WRITE '@KERNEL oref.set(this.mv_ref.deref());'.
ENDMETHOD.
ENDCLASS.
54 changes: 54 additions & 0 deletions src/abap/cl_abap_weak_reference.clas.testclasses.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
CLASS ltcl_dummy DEFINITION FOR TESTING.
ENDCLASS.

CLASS ltcl_dummy IMPLEMENTATION.
ENDCLASS.

******************************

CLASS ltcl_weak_reference DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT FINAL.

PRIVATE SECTION.
METHODS basic FOR TESTING.
METHODS collected FOR TESTING.

METHODS method RETURNING VALUE(weak) TYPE REF TO cl_abap_weak_reference.

ENDCLASS.

CLASS ltcl_weak_reference IMPLEMENTATION.

METHOD basic.

DATA ref TYPE REF TO ltcl_dummy.
DATA weak TYPE REF TO cl_abap_weak_reference.

CREATE OBJECT ref.
CREATE OBJECT weak EXPORTING oref = ref.

cl_abap_unit_assert=>assert_equals(
act = weak->get( )
exp = ref ).

ENDMETHOD.

METHOD method.
DATA ref TYPE REF TO ltcl_dummy.
CREATE OBJECT ref.
CREATE OBJECT weak EXPORTING oref = ref.
ENDMETHOD.

METHOD collected.
DATA weak TYPE REF TO cl_abap_weak_reference.

weak = method( ).

* https://github.com/orgs/nodejs/discussions/36467
WRITE '@KERNEL await new Promise(resolve => setTimeout(resolve, 0));;'.
* works only on NodeJS, with --expose-gc
WRITE '@KERNEL global.gc();'.

cl_abap_unit_assert=>assert_initial( weak->get( ) ).
ENDMETHOD.

ENDCLASS.
54 changes: 54 additions & 0 deletions src/rtti/cl_abap_typedescr.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ CLASS ltcl_test DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT FINAL.
METHODS structure_absolute FOR TESTING.

METHODS tab_length FOR TESTING.
METHODS identical_refs1 FOR TESTING.
METHODS identical_refs2 FOR TESTING.
METHODS identical_refs3 FOR TESTING.
METHODS identical_refs4 FOR TESTING.

ENDCLASS.

Expand Down Expand Up @@ -623,4 +627,54 @@ CLASS ltcl_test IMPLEMENTATION.
exp = 8 ).
ENDMETHOD.

METHOD identical_refs1.
" DATA t TYPE timestamp.
" DATA ref1 TYPE REF TO cl_abap_typedescr.
" DATA ref2 TYPE REF TO cl_abap_typedescr.
" ref1 = cl_abap_typedescr=>describe_by_data( t ).
" ref2 = cl_abap_typedescr=>describe_by_data( t ).
" cl_abap_unit_assert=>assert_equals(
" act = ref1
" exp = ref2 ).
ENDMETHOD.

METHOD identical_refs2.
" TYPES ty TYPE c LENGTH 2.
" DATA t1 TYPE ty.
" DATA t2 TYPE ty.
" DATA ref1 TYPE REF TO cl_abap_typedescr.
" DATA ref2 TYPE REF TO cl_abap_typedescr.
" ref1 = cl_abap_typedescr=>describe_by_data( t1 ).
" ref2 = cl_abap_typedescr=>describe_by_data( t2 ).
" cl_abap_unit_assert=>assert_equals(
" act = ref1
" exp = ref2 ).
ENDMETHOD.

METHOD identical_refs3.
" DATA t1 TYPE c LENGTH 2.
" DATA t2 TYPE c LENGTH 2.
" DATA ref1 TYPE REF TO cl_abap_typedescr.
" DATA ref2 TYPE REF TO cl_abap_typedescr.
" ref1 = cl_abap_typedescr=>describe_by_data( t1 ).
" ref2 = cl_abap_typedescr=>describe_by_data( t2 ).
" cl_abap_unit_assert=>assert_equals(
" act = ref1
" exp = ref2 ).
ENDMETHOD.

METHOD identical_refs4.
" TYPES ty1 TYPE c LENGTH 2.
" TYPES ty2 TYPE c LENGTH 2.
" DATA t1 TYPE ty1.
" DATA t2 TYPE ty2.
" DATA ref1 TYPE REF TO cl_abap_typedescr.
" DATA ref2 TYPE REF TO cl_abap_typedescr.
" ref1 = cl_abap_typedescr=>describe_by_data( t1 ).
" ref2 = cl_abap_typedescr=>describe_by_data( t2 ).
" IF ref1 = ref2.
" cl_abap_unit_assert=>fail( ).
" ENDIF.
ENDMETHOD.

ENDCLASS.
4 changes: 2 additions & 2 deletions src/unit/cl_abap_unit_assert.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ CLASS cl_abap_unit_assert DEFINITION PUBLIC.
CLASS-METHODS
assert_differs
IMPORTING
act TYPE any
exp TYPE any
act TYPE simple
exp TYPE simple
msg TYPE csequence OPTIONAL
quit TYPE i OPTIONAL
level TYPE i OPTIONAL.
Expand Down
7 changes: 5 additions & 2 deletions src/unit/cl_abap_unit_assert.clas.locals_imp.abap
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
* Note: dont reuse RTTI here, would like to keep it
* possible to run unit tests without RTTI if possible
* Note: dont reuse RTTI here, would like to keep it possible to run unit tests without RTTI
CLASS lcl_dump DEFINITION.
PUBLIC SECTION.
CLASS-METHODS to_string
Expand All @@ -14,6 +13,7 @@ ENDCLASS.
CLASS lcl_dump IMPLEMENTATION.
METHOD to_string.
DATA lv_type TYPE c LENGTH 1.
DATA lv_name TYPE string.

DESCRIBE FIELD iv_val TYPE lv_type.
CASE lv_type.
Expand All @@ -22,6 +22,9 @@ CLASS lcl_dump IMPLEMENTATION.
rv_str = dump_structure( iv_val ).
WHEN 'h'.
rv_str = |[itab]|.
WHEN 'r'.
WRITE '@KERNEL lv_name.set(iv_val.get().constructor.name);'.
rv_str = |[object, { lv_name }]|.
WHEN OTHERS.
rv_str = |{ iv_val }|.
ENDCASE.
Expand Down
11 changes: 10 additions & 1 deletion src/unit/osql/cl_osql_test_environment.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CLASS cl_osql_test_environment DEFINITION PUBLIC.
DATA mo_sql TYPE REF TO cl_sql_statement.

METHODS initialize.
METHODS set_runtime_prefix.
ENDCLASS.

CLASS cl_osql_test_environment IMPLEMENTATION.
Expand All @@ -39,7 +40,6 @@ CLASS cl_osql_test_environment IMPLEMENTATION.
DATA lv_sql TYPE string.
DATA lo_result TYPE REF TO cl_sql_result_set.
DATA lr_ref TYPE REF TO data.
DATA lv_extra TYPE string.

WRITE '@KERNEL if (abap.dbo.schemaPrefix !== "") throw new Error("already prefixed");'.

Expand All @@ -63,6 +63,13 @@ CLASS cl_osql_test_environment IMPLEMENTATION.
mo_sql->execute_update( lv_sql ).
ENDLOOP.

set_runtime_prefix( ).

ENDMETHOD.

METHOD set_runtime_prefix.

DATA lv_extra TYPE string.
lv_extra = |'.'|.
WRITE '@KERNEL abap.dbo.schemaPrefix = this.mv_schema.get() + lv_extra.get();'.

Expand Down Expand Up @@ -91,6 +98,8 @@ CLASS cl_osql_test_environment IMPLEMENTATION.
DATA lo_struct_descr TYPE REF TO cl_abap_structdescr.
DATA lv_table TYPE string.

set_runtime_prefix( ).

lo_table_descr ?= cl_abap_typedescr=>describe_by_data( i_data ).
lo_struct_descr ?= lo_table_descr->get_table_line_type( ).
lv_table = lo_struct_descr->get_relative_name( ).
Expand Down

0 comments on commit 409a039

Please sign in to comment.