Skip to content

Commit

Permalink
implement experimental:vtableMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Nov 29, 2023
1 parent 0aca90f commit f4a660d
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

- `-d:nimStrictDelete` becomes the default. An index error is produced when the index passed to `system.delete` was out of bounds. Use `-d:nimAuditDelete` to mimic the old behavior for backwards compatibility.
- The default user-agent in `std/httpclient` has been changed to `Nim-httpclient/<version>` instead of `Nim httpclient/<version>` which was incorrect according to the HTTP spec.
- Methods now support implementations based on vtable by using `-d:nimPreviewVtables`. methods are confined in the same module where the type has been defined.
- Methods now support implementations based on vtable by using `--experimental:vtableMethods`. methods are confined in the same module where the type has been defined.
- With `-d:nimPreviewNonVarDestructor`, non-var destructors become the default.

## Standard library additions and changes
Expand Down
2 changes: 1 addition & 1 deletion compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2234,7 +2234,7 @@ proc finalCodegenActions*(graph: ModuleGraph; m: BModule; n: PNode) =
incl m.flags, objHasKidsValid
if optMultiMethods in m.g.config.globalOptions or
m.g.config.selectedGC notin {gcArc, gcOrc, gcAtomicArc} or
not m.g.config.isDefined("nimPreviewVtables"):
vtableMethods notin m.g.config.features:
generateIfMethodDispatchers(graph, m.idgen)


Expand Down
2 changes: 1 addition & 1 deletion compiler/cgmeth.nim
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ proc fixupDispatcher(meth, disp: PSym; conf: ConfigRef) =

proc methodDef*(g: ModuleGraph; idgen: IdGenerator; s: PSym) =
var witness: PSym = nil
if s.typ[1].owner.getModule != s.getModule and g.config.isDefined("nimPreviewVtables"):
if s.typ[1].owner.getModule != s.getModule and vtableMethods in g.config.features and not g.config.isDefined("nimInternalNonVtablesTesting"):
localError(g.config, s.info, errGenerated, "method `" & s.name.s &
"` can be defined only in the same module with its type (" & s.typ[1].typeToString() & ")")
for i in 0..<g.methods.len:
Expand Down
2 changes: 2 additions & 0 deletions compiler/condsyms.nim
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,5 @@ proc initDefines*(symbols: StringTableRef) =

defineSymbol("nimHasCastExtendedVm")
defineSymbol("nimHasWarnStdPrefix")

defineSymbol("nimHasVtables")
5 changes: 4 additions & 1 deletion compiler/nim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ define:nimPreviewSlimSystem
define:nimPreviewCstringConversion
define:nimPreviewProcConversion
define:nimPreviewRangeDefault
define:nimPreviewVtables
define:nimPreviewNonVarDestructor
threads:off

Expand Down Expand Up @@ -58,3 +57,7 @@ define:useStdoutAsStdmsg
warning[StdPrefix]:on
warningAsError[StdPrefix]:on
@end

@if nimHasVtables:
experimental:vtableMethods
@end
3 changes: 2 additions & 1 deletion compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ type
flexibleOptionalParams,
strictDefs,
strictCaseObjects,
inferGenericTypes
inferGenericTypes,
vtableMethods

LegacyFeature* = enum
allowSemcheckedAstModification,
Expand Down
2 changes: 1 addition & 1 deletion compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ proc semStmtAndGenerateGenerics(c: PContext, n: PNode): PNode =
trackStmt(c, c.module, result, isTopLevel = true)
if optMultiMethods notin c.config.globalOptions and
c.config.selectedGC in {gcArc, gcOrc, gcAtomicArc} and
c.config.isDefined("nimPreviewVtables"):
vtableMethods in c.config.features:
sortVTableDispatchers(c.graph)

if sfMainModule in c.module.flags:
Expand Down
2 changes: 1 addition & 1 deletion lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1606,7 +1606,7 @@ when not defined(js) and defined(nimV2):
traceImpl: pointer
typeInfoV1: pointer # for backwards compat, usually nil
flags: int
when defined(nimPreviewVtables):
when defined(gcDestructors):
when defined(cpp):
vTable: ptr UncheckedArray[pointer] # vtable for types
else:
Expand Down
7 changes: 3 additions & 4 deletions lib/system/arc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ template tearDownForeignThreadGc* =
proc isObjDisplayCheck(source: PNimTypeV2, targetDepth: int16, token: uint32): bool {.compilerRtl, inl.} =
result = targetDepth <= source.depth and source.display[targetDepth] == token

when defined(nimPreviewVtables):
proc nimGetVTable(p: pointer, index: int): pointer
{.compilerRtl, inline, raises: [].} =
result = cast[ptr PNimTypeV2](p).vTable[index]
proc nimGetVTable(p: pointer, index: int): pointer
{.compilerRtl, inline, raises: [].} =
result = cast[ptr PNimTypeV2](p).vTable[index]
3 changes: 1 addition & 2 deletions tests/config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ switch("define", "nimPreviewNonVarDestructor")

switch("warningAserror", "UnnamedBreak")
switch("legacy", "verboseTypeMismatch")
switch("define", "nimPreviewVtables")

switch("experimental", "vtableMethods")
2 changes: 1 addition & 1 deletion tests/generics/tobjecttyperel.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "-u:nimPreviewVtables"
matrix: "-d:nimInternalNonVtablesTesting"
output: '''(peel: 0, color: 15)
(color: 15)
17
Expand Down
2 changes: 1 addition & 1 deletion tests/method/tgeneric_methods.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "--mm:arc --multimethods:on -u:nimPreviewVtables; --mm:refc --multimethods:on -u:nimPreviewVtables"
matrix: "--mm:arc --multimethods:on -d:nimInternalNonVtablesTesting; --mm:refc --multimethods:on -d:nimInternalNonVtablesTesting"
output: '''wow2
X 1
X 3'''
Expand Down
2 changes: 1 addition & 1 deletion tests/method/tmethods_old.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "--mm:arc -u:nimPreviewVtables"
matrix: "--mm:arc -d:nimInternalNonVtablesTesting"
output: '''
do nothing
'''
Expand Down

0 comments on commit f4a660d

Please sign in to comment.