Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
^renovate\.json$
^doc$
^Meta$
^sub$
87 changes: 87 additions & 0 deletions sub/PACKAGE_STRUCTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Constructive Package Structure

## Overview

This directory contains extracted sub-packages from the constructive ecosystem.

## constructive.core

The `constructive.core` package contains the core infrastructure for code generation:

### Included Components

#### Infrastructure Files (34 files)
- Core functions: `construct.R`, `construct_*.R` (clip, diff, dput, dump, reprex, signature)
- Helper utilities: `construct-helpers.R`, `character-helpers.R`, `deparse_*.R`, `format_atomic.R`
- Options system: `opts.R`, `global-options.R`
- Attribute handling: `repair_attributes.R`, `contains_self_reference.R`
- Templates: `templates.R`, `custom_constructors.R`
- Package infrastructure: `constructive-package.R`, `zzz.R`, `00_meta.R`
- Error handling: `abort.R`, `bypass.R`
- Other utilities: `utils.R`, `environment_utils.R`, `s3_register.R`, `roxygen2-tags.R`
- Documentation: `document-other-opts.R`, `expect_construct.R`
- Lifecycle management: `import-standalone-lifecycle.R`

#### Base Type Methods (18 files)
Files implementing constructors for base R types:
- `s3-NULL.R` - NULL type
- `s3-atomic.R` - Base atomic vectors
- `s3-character.R` - Character vectors
- `s3-integer.R` - Integer vectors
- `s3-double.R` - Double/numeric vectors
- `s3-complex.R` - Complex numbers
- `s3-logical.R` - Logical vectors
- `s3-raw.R` - Raw vectors
- `s3-list.R` - Lists
- `s3-array.R` - Arrays
- `s3-matrix.R` - Matrices
- `s3-environment.R` - Environments
- `s3-function.R` - Functions
- `s3-language.R` - Language objects (calls, expressions)
- `s3-pairlist.R` - Pairlists
- `s3-dots.R` - ... (dots)
- `s3-externalptr.R` - External pointers
- `s3-object.R` - Object type

#### S4 Support
- `s4.R` - S4 object construction

#### C++ Code
- `src/constructive.cpp` - C functions for external pointer handling and environment retrieval

#### Tests
26 test files covering core functionality:
- Infrastructure tests: `test-construct-*.R`, `test-deparse_call.R`, `test-opts.R`, etc.
- Type tests: `test-s3-array.R`, `test-s3-list.R`, `test-s3-environment.R`, etc.
- Utility tests: `test-utils.R`, `test-repair_attributes.R`, etc.

### Purpose

The `constructive.core` package serves as a lightweight, low-dependency foundation that:
1. Provides the default `.cstr_construct()` method
2. Handles all base R types
3. Supplies infrastructure functions used by extension packages
4. Can be independently installed and tested

### Dependencies

Imports:
- cli (>= 3.1.0)
- diffobj
- methods
- rlang (>= 1.0.0)
- waldo

## Future Packages

Additional packages will be extracted in future iterations:
- `constructive.base` - Base R class methods (Date, POSIXct, factor, data.frame, etc.)
- `constructive.ggplot2` - ggplot2 constructors
- `constructive.methods` - Other package-specific methods

## Notes

Currently, the main `constructive` package still contains all code. The extraction to `constructive.core` is the first step in the modularization process. Future work will:
1. Update the main package to import from `constructive.core`
2. Remove duplicated code from the main package
3. Extract additional specialized packages
3 changes: 3 additions & 0 deletions sub/constructive.core/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
^constructive\.core\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
34 changes: 34 additions & 0 deletions sub/constructive.core/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Package: constructive.core
Title: Core Infrastructure for Constructive Code Generation
Version: 0.1.0
Authors@R: c(
person("Antoine", "Fabri", , "[email protected]", role = c("aut", "cre")),
person("Kirill", "Müller", , "[email protected]", role = "ctb",
comment = c(ORCID = "0000-0002-1416-3412")),
person("Jacob", "Scott", , "[email protected]", role = "ctb"),
person("cynkra GmbH", , , "[email protected]", role = "fnd",
comment = c(ROR = "0335t7e62"))
)
Description: Provides core infrastructure for generating R code that can
recreate R objects. This package contains the default method and
essential helper functions used by the 'constructive' package ecosystem.
License: MIT + file LICENSE
URL: https://github.com/cynkra/constructive
BugReports: https://github.com/cynkra/constructive/issues
Imports:
cli (>= 3.1.0),
diffobj,
methods,
rlang (>= 1.0.0),
waldo
Suggests:
clipr,
reprex,
rstudioapi,
S7,
testthat (>= 3.0.0),
withr
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.3.9000
2 changes: 2 additions & 0 deletions sub/constructive.core/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2023
COPYRIGHT HOLDER: constructive authors
21 changes: 21 additions & 0 deletions sub/constructive.core/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2023 constructive authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions sub/constructive.core/R/00_meta.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# functions used to build functions of the package
new_constructive_opts_function <- function(class, constructors, ...) {

env <- parent.frame()
DOTS <- eval(substitute(alist(...)))
CONSTRUCTOR <- substitute(constructors)
FORWARDED_DOTS <- DOTS
FORWARDED_DOTS[] <- lapply(names(DOTS), as.symbol)
CLASS <- substitute(class)
eval(bquote(
splice = TRUE,
as.function(
alist(constructor = .(CONSTRUCTOR), ... =, ..(DOTS), {
.cstr_combine_errors(
constructor <- .cstr_match_constructor(constructor, .(CLASS)),
check_dots_empty()
)
.cstr_options(.(CLASS), constructor = constructor, ..(FORWARDED_DOTS))
}),
envir = env
)
))
}

new_constructive_method <- function(class, constructors, ...) {
env <- parent.frame()
CLASS <- substitute(class)
CLASS_CHR <- as.character(CLASS)
IS_CORRUPTED_FUN <- as.symbol(paste0("is_corrupted_", class))
CONSTRUCTOR_ARGS <- sapply(constructors, as.symbol)
DOTS <- eval(substitute(alist(...)))
FORWARDED_DOTS <- DOTS
FORWARDED_DOTS[] <- lapply(names(DOTS), function(x) call("$", quote(opts), as.symbol(x)))
OPTS_FUN = as.symbol(sprintf("opts_%s", CLASS_CHR))
eval(bquote(
splice = TRUE,
as.function(
alist(x = , opts =, ... = ,{
opts_local <- opts[[.(CLASS_CHR)]] %||% .(OPTS_FUN)()
if (.(IS_CORRUPTED_FUN)(x) || opts_local[["constructor"]] == "next") return(NextMethod())
constructor <- constructors[[.(CLASS)]][[opts_local[["constructor"]]]]
constructor(x, opts = opts, ..(FORWARDED_DOTS), ...)
}),
envir = env
)
))
}
34 changes: 34 additions & 0 deletions sub/constructive.core/R/C_wrappers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' @useDynLib constructive.core
NULL

#' Build a pointer from a memory address
#'
#' Base R doesn't provide utilities to build or manipulate external pointers
#' (objects of type "externalptr"), so we provide our own.
#' Objects defined with `.xptr()` are not stable across sessions,
#'
#' @param address Memory address
#' @return The external pointer (type "externalptr") that the memory address points to.
#' @export
.xptr <- function(address) {
if (!missing(address) && is.character(address) && length(address) == 1L) {
ptr <- globals[["external_pointers"]][[address]]
if (!is.null(ptr)) return(ptr)
}
abort(paste0("No external pointer registered for key ", address))
}

external_pointer_address <- function(s) {
if (identical(Sys.getenv("TESTTHAT"), "true")) return("0x123456789")
.Call("external_pointer_address", PACKAGE = "constructive.core", s)
}

env_impl <- function(address) {
.Call("objectFromAddress", PACKAGE = "constructive.core", address)
}

promise_env <- function(name, env = parent.frame()) {
enquo_call <- substitute(rlang::enquo(X), list(X = rlang::sym(name)))
quo <- eval(enquo_call, env)
rlang::quo_get_env(quo)
}
Loading